我正在嘗試創建一個函式,以串列形式將 csv 檔案讀入記憶體。當我運行我的代碼時,它給了我這個錯誤訊息(“字串索引必須是整數”)。我是不是弄錯了。下面是代碼。謝謝你的幫助
# create the empty set to carry the values of the columns
Hydropower_heading = []
Solar_heading = []
Wind_heading = []
Other_heading = []
def my_task1_file(filename): # defines the function "my_task1_file"
with open(filename,'r') as myNew_file: # opens and read the file
for my_file in myNew_file.readlines(): # loops through the file
# read the values into the empty set created
Hydropower_heading.append(my_file['Hydropower'])
Solar_heading.append(my_file['Solar'])
Wind_heading.append(my_file['Wind'])
Other_heading.append(my_file['Other'])
#Hydropower_heading = int(Hydropower)
#Solar_heading = int(Solar)
#Wind_heading = int(Wind)
#Other_heading = int(Other)
my_task1_file('task1.csv') # calls the csv file into the function
# print the Heading and the column values in a row form
print('Hydropower: ', Hydropower_heading)
print('Solar: ', Solar_heading)
print('Wind: ', Wind_heading)
print('Other: ', Other_heading)
uj5u.com熱心網友回復:
我們可以使用csv.DictReader 方法按列讀取 CSV 檔案。
代碼: ( code.py)
import csv
def my_task1_file(filename): # defines the function "my_task1_file"
Hydropower_heading = []
Solar_heading = []
Wind_heading = []
Other_heading = []
with open(filename, newline='\n') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# read the values into the empty set created
Hydropower_heading.append(row['Hydropower'])
Solar_heading.append(row['Solar'])
Wind_heading.append(row['Wind'])
Other_heading.append(row['Other'])
return Hydropower_heading, Solar_heading, Wind_heading, Other_heading
if __name__ == "__main__":
Hydropower_heading, Solar_heading, Wind_heading, Other_heading = my_task1_file('task1.csv')
# print the Heading and the column values in a row form
print('Hydropower: ', Hydropower_heading)
print('Solar: ', Solar_heading)
print('Wind: ', Wind_heading)
print('Other: ', Other_heading)
task1.csv:
Hydropower,Solar,Wind,Other
5,6,3,8
6,8,5,12
3,6,9,7
輸出:
Hydropower: ['5', '6', '3']
Solar: ['6', '8', '6']
Wind: ['3', '5', '9']
Other: ['8', '12', '7']
解釋:
- 該
__main__條件將檢查檔案是否直接運行。如果檔案是通過 using 直接運行的python code.py,它將執行這部分。否則如果我們code.py從另一個python檔案匯入,這部分將不會被執行。 - 您可以
__main__根據需要洗掉塊,如下所示。但是,在使用__main__塊從另一個 python 檔案匯入一個 python 檔案時,將方法與執行分開是一種很好的做法。如果它消除了您的困惑,請告訴我。
code.py(沒有__main__):
import csv
def my_task1_file(filename): # defines the function "my_task1_file"
Hydropower_heading = []
Solar_heading = []
Wind_heading = []
Other_heading = []
with open(filename, newline='\n') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
# read the values into the empty set created
Hydropower_heading.append(row['Hydropower'])
Solar_heading.append(row['Solar'])
Wind_heading.append(row['Wind'])
Other_heading.append(row['Other'])
return Hydropower_heading, Solar_heading, Wind_heading, Other_heading
Hydropower_heading, Solar_heading, Wind_heading, Other_heading = my_task1_file('task1.csv')
print('Hydropower: ', Hydropower_heading)
print('Solar: ', Solar_heading)
print('Wind: ', Wind_heading)
print('Other: ', Other_heading)
參考:
- csv.DictReader 方法
__main__來自 Python 網站的檔案
uj5u.com熱心網友回復:
由于錯誤是“字串索引必須是整數”,因此您必須使用不能將字串值作為索引的資料型別。在這段代碼中...
for my_file in myNew_file.readlines():
Hydropower_heading.append(my_file['Hydropower'])
Solar_heading.append(my_file['Solar'])
Wind_heading.append(my_file['Wind'])
Other_heading.append(my_file['Other'])
...您正在使用“Hydropower”、“Solar”、“Wind”和“Other”作為索引值,它們不能是 my_file 的有效索引值,我假設它是字串資料型別,因為您正在閱讀檔案myNew_file。如果您適當地將這些值更改為整數,則該錯誤不應再出現。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/401830.html
