[英]How to take inputs from a CSV file and write a specific output with 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.