簡體   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