原始格式是txt文件,格式為:內(nèi)華達(dá)州拉斯維加斯|36.17497|-115.13722該txt包含大約70行,格式相同目標(biāo)字典格式應(yīng)為 {“城市名稱”:[緯度, 經(jīng)度]}感謝您的回答之一,但仍然陷入困境:with open("cities.txt") as f: txt = f.read()lines = txt.split('\n')my_dict = dict()for line in lines: city,lat,lng = txt.split('|') my_dict[city] = [lat,lng] 它顯示: ValueError: 太多值無(wú)法解壓(預(yù)期為 3),接下來(lái)我應(yīng)該修改什么?我將粘貼txt文件的格式如下:las vegas,nv|36.17497|-115.13722homestead,pa|40.4059|-79.91199streetsboro,oh|41.23922|-81.34594
2 回答

繁花如伊
TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊
with open(my_file.txt) as f:
lines = f.readlines()
my_dict = dict()
for line in lines:
city,lat,lng = line.split('|')
my_dict[city] = [lat,lng]
添加回答
舉報(bào)
0/150
提交
取消