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