我正在尝试使用python将文本文件读入字典。当我打开文件时,其内容如下:
SS,City,State,Country,Pop,Age,Random
('321', 'Houston', 'TX', 'US', '84549', '45', 2000)
('654', 'Miami', 'FL', 'US', '99999', '55', -2001)
('940', 'Dallas', 'TX', 'US', '3243', '30', 324113)
当我将文件打开成字典时,我得到的是文字文件中看不到的字符。我已经绑定了剥离和删除字符,但是似乎什么也无法工作。当我打印字典时,会发生以下情况:
("('321'", " 'Houston'"," 'TX'"," 'US'"," '84549'"," '45'",' 2000)')
("('654'"," 'Miami'"," 'FL'"," 'US'"," '99999'"," '55'"," -2001)')
("('940'"," 'Dallas'"," 'TX'"," 'US'"," '3243'"," '30'"," 324113)')
下面是我到目前为止的代码。
locations={}
with open ("locations.txt") as lct:
z=lct.readline()
for line in lct:
line=line.strip().split(",")
ss, city, state, cntry, pop, age, random = line
if state == "TX":
locations[ss] = Texas(ss,city,state,cntry,pop,age,random)
elif state == "FL":
locations[ss] = Florida(ss,city,state,cntry,pop,age,random)
我希望这些行显示如下:
(“ 321”,“休斯顿”,“ TX”,“美国”,“ 84549”,“ 45”,“ 2000”)
有什么建议吗?
答案 0 :(得分:0)
您可以将传入的字符串切成薄片。
locations={}
with open ("locations.txt") as lct:
z=lct.readline()
for line in lct:
line=line.strip()[1:-1].split(",")
ss, city, state, cntry, pop, age, random = line
if state == "TX":
locations[ss] = Texas(ss,city,state,cntry,pop,age,random)
elif state == "FL":
locations[ss] = Florida(ss,city,state,cntry,pop,age,random)
答案 1 :(得分:0)
文件只是一堆文本,其中还考虑了空格和倒置 使用正则表达式可以帮助您
import re
text = "('321', 'Houston', 'TX', 'US', '84549', '45', 2000)"
pattern = r"(\w+)"
print(re.findall(pattern,text))
>["321', 'Houston', 'TX', 'US', '84549', '45", '2000']
所以您的代码看起来像
import re #Added line
pattern = r"(\w+)" #Added line
locations={}
with open ("locations.txt") as lct:
z=lct.readline()
for line in lct:
l = re.findall(pattern,line) #changed line
ss, city, state, cntry, pop, age, random = l
if state == "TX":
locations[ss] = Texas(ss,city,state,cntry,pop,age,random)
elif state == "FL":
locations[ss] = Florida(ss,city,state,cntry,pop,age,random)
答案 2 :(得分:0)
只需将三个字符串1.(2.)3.'替换为空字符串,即可解决问题。
请使用以下代码
locations={}
with open ("locations.txt") as lct:
z=lct.readline()
for line in lct:
line.replace("(","")
line.replace(")","")
line.replace("'","")
line=line.strip().split(",")
ss, city, state, cntry, pop, age, random = line
if state == "TX":
locations[ss] = Texas(ss,city,state,cntry,pop,age,random)
elif state == "FL":
locations[ss] = Florida(ss,city,state,cntry,pop,age,random)
line=line.strip().split(",")
ss, city, state, cntry, pop, age, random = line
if state == "TX":
locations[ss] = Texas(ss,city,state,cntry,pop,age,random)
elif state == "FL":
locations[ss] = Florida(ss,city,state,cntry,pop,age,random)
答案 3 :(得分:0)
由于文本格式符合Python语法,因此使用eval会很容易。
text = """('321', 'Houston', 'TX', 'US', '84549', '45', 2000)
('654', 'Miami', 'FL', 'US', '99999', '55', -2001)
('940', 'Dallas', 'TX', 'US', '3243', '30', 324113)"""
locations={}
func = {'TX':Texes, 'FL':Florida}
for line in text.split('\n'):
args = eval(line)
ss, state = args[0], args[2]
if state in func:
locations[ss] = func(*args)