我當前的 csv 檔案:
'Date','Category','Ability'
'21,14,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'
'21,14,5','Penguin','water,land'
我的代碼:
Living_beings=[]
with open(users_read,'r') as f:
reader=DictReader(f)
for row in reader:
if date.today().day in row['Date']:
Living_beings =row['Category']
print(Living_beings)
輸出 ;['S','p','a','r','r','o','w','P','e','n','g','u','i','n']
預期輸出:[Sparrow, penguin]
我不知道它為什么被分開......關于這個的任何想法。
uj5u.com熱心網友回復:
該行將Living_beings =row['Category']的內容row['Category']視為字串列(因為它是一個字串),并Living_beings使用字串列擴展該串列row['Category']。
相反,您應該使用:
Living_beings.append(row['Category'])
uj5u.com熱心網友回復:
試試Living_beings.append(row['Category'])吧。
我懷疑原始代碼被row['Category']視為單個字符的串列,因此它可以組合串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/491256.html
