[英]Reading and formatting text files into Python
I am trying to read a text file into a dictionary using python.我正在尝试使用 python 将文本文件读入字典。 When I open the file, it reads as follows:
当我打开文件时,它显示如下:
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)
When I go to open my file into a dictionary I am getting added characters that I do not see in the text file.当我 go 将我的文件打开到字典中时,我正在添加我在文本文件中看不到的字符。 I have tied stripping and removing characters but can't seem to get anything to work.
我已经绑定了剥离和删除字符,但似乎无法正常工作。 Here is what happens when I print my dictionary:
这是我打印字典时发生的情况:
("('321'", " 'Houston'"," 'TX'"," 'US'"," '84549'"," '45'",' 2000)')
("('654'"," 'Miami'"," 'FL'"," 'US'"," '99999'"," '55'"," -2001)')
("('940'"," 'Dallas'"," 'TX'"," 'US'"," '3243'"," '30'"," 324113)')
Below is the code I have so far.以下是我到目前为止的代码。
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)
I would like the lines to display as follows:我希望这些行显示如下:
('321', 'Houston', 'TX', 'US', '84549', '45', '2000') (“321”、“休斯顿”、“TX”、“美国”、“84549”、“45”、“2000”)
Any suggestions?有什么建议么?
You can just slice the incoming string.您可以对传入的字符串进行切片。
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)
the file is just a bunch of text where space and inverted also considered using a regex can help you该文件只是一堆文本,其中空格和倒置也考虑使用正则表达式可以帮助您
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']
so your code will look like所以你的代码看起来像
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)
Just replace the three string 1. ( 2. ) 3. ' with empty string your problem will be solved.只需将三个字符串 1. ( 2. ) 3. ' 替换为空字符串,您的问题就会解决。
Please use the below code请使用以下代码
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)
Since the format of text meet Python syntax, use eval will be easy.由于文本格式符合 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.