0
- 爲什麼刪除雙引號不起作用? (國家=國家[1:-1])
- 爲什麼
first_list.append
行不能執行?我正在遍歷輸入文件的行並將它與存儲在final_lists中的列表的第一個元素進行匹配。當我打印輸出時,即使這兩個值實際上與打印語句驗證的相同(例如,行[0] ==津巴布韋和country ==津巴布韋),下一個附加語句也不會運行。
with open('world_bank_regions.tsv', 'rU') as f:
next(f)
for line in f:
[region, subregion, country] = line.split('\t')
if country.startswith('"') and country.endswith('"'):
country = country[1:-1]
print country #the double quotes remain
for row in final_list: #final list is a list of lists
print row[0] #row[0] == Zimbabwe
print country #country == Zimbabwe
if row[0] == country:
final_list.append([region, subregion])
print final_list #no changes were made to the list from the previous steps
是什麼數據是什麼樣子?任何不使用'csv'模塊的原因,它可以明智地處理製表符分隔的文件。 – AChampion
@AChampion來自輸入文件的數據只是三列字符串。我沒有使用csv模塊,因爲當我早些時候嘗試這樣做時,我無法弄清楚如何直接將它們分配給變量,如上所述(即,將每行指定爲'region''subregion''國家'),而是不得不只是將每一行分配給列表 –
'對於地區,分區域,國家在csv_reader:'將允許您迭代一個csv文件,將每行分配給變量'region','subregion''' country '。你能分享幾行數據文件嗎? – AChampion