繁体   English   中英

如何从CSV中获取特定表并在Python中编写新文件?

[英]How do I take specific tables from a CSV and write a new file in Python?

我想从CSV文件中获取特定表,并为每个表返回一个文件。 我有一些看起来像这样的东西:

France    city    population    agriculture
France    Paris    2000000      lots
France    Nice     500000      some


England   city    population    agriculture
England   London    30000       none
England   Glasgow    10000      some

我想要返回两个文件,一个用

France    city    population    agriculture
France    Paris    2000000      lots
France    Nice     500000      some

和另一个

England   city    population    agriculture
England   London    30000       none
England   Glasgow    10000      some

我该怎么做呢?

这是一个不使用cvs模块的解决方案(可以将csv模块分开表吗?)

with open('table.txt') as f:
    text = f.read()

tables = text.split('\n\n')

for itable,table in enumerate(tables):
    fileout = 'table%2.2i.txt' % itable
    with open(fileout,'w') as f:
        f.write(table.strip())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM