這是代碼,我想看看我做錯了什么。我是 python 函式和鏈接外部檔案的新手,所以如果你能解釋你的代碼會很好。
def get_data(filename):
records = []
with open(filename) as readfile:
lines = readfile.readlines()
for line in lines:
# variable line contains:
str_rec = line.split(",")
pname = str_rec[0]
price = int(str_rec[1])
quantity = int(str_rec[2])
records.append([pname, price, quantity])
#caution: indentation
return records
hell= get_data(data.txt)
print(hell)
data.txt 是我試圖作為引數傳遞的另一個檔案的鏈接。
uj5u.com熱心網友回復:
open(filename) 將檔案名作為字串傳遞,因此您應該將名稱作為字串傳遞,而不是實際檔案。
hell= get_data("data.txt")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487541.html
