用Python解析文件
我有以下格式的文件:用Python解析文件,python,parsing,python-3.x,Python,Parsing,Python 3.x,我有以下格式的文件: Berlin, Germany New Delhi , India New York , USA Mumbai , India Seattle, USA 我需要解析文件并将输出打印为 Germany : Berlin India: New Delhi , Mumbai USA: New York, Seattle 我写了一段代码: enter code here: def check(): datafile=open('logfile.py','rU
Berlin, Germany
New Delhi , India
New York , USA
Mumbai , India
Seattle, USA
我需要解析文件并将输出打印为
Germany : Berlin
India: New Delhi , Mumbai
USA: New York, Seattle
我写了一段代码:
enter code here:
def check():
datafile=open('logfile.py','rU')
found=False
for line in datafile:
if 'India' in line:
lines=line.split()
print("India"+":"+lines[0])
if 'Germany' in line:
lines=line.split()
print("Germany"+":"+lines[0])
if 'USA' in line:
lines=line.split()
print("USA"+":"+lines[0])
datafile.close()
check()
此代码的输出为:
Germany:Berlin
India:NewDelhi
USA:NewYork
India:Mumbai
USA:Seattle
请提供帮助。您可以将其保存到数据结构中,如字典或集合。defaultdict,而不是直接打印所有内容
import collections.defaultdict as dd
result = dd(list)
with open('logfile.py', 'rU') as datafile:
for line in datafile:
city,country = map(str.strip, line.strip().split(','))
result[country].append(city)
然后打印结果:
for country in result:
print(country+':', ', '.join(result[country]))
如果您认为可能存在重复的国家/城市列表,并且您不想要它们,请使用
set
和add
而不是list
和append
另一种方法是使用defaultdict
fromcollections
实现此目的:
from collections import defaultdict
def check():
d = defaultdict(list)
with open('logfile.py', 'rU') as datafile:
for line in datafile:
data = line.split(',')
d[data[1].strip()].append(data[0].strip())
return d
res = check()
for k, v in res.items():
print("{} : {}".format(k, ', '.join(v)))
输出:
India : New Delhi, Mumbai
Germany : Berlin
USA : New York, Seattle
您是如何执行默认的
split()
并丢失逗号的?从第一个答案中可以看出,您必须列出所有国家/地区的列表。因此,诀窍是启动一个国家列表,然后确定一个国家是否已经存在,然后将该城市附加到国家列表中。@roadrunner66-collections.defaultdict
为您管理所有这些you@PaulMcGuire(泰:)很好——现在我想知道如何在原始的“必需”列表中获得不合理的间距。