我试图合并了一些CSV文件。 我最初的功能旨在:
- 看一个目录里并计算文件中的数字(假设都是的.csv)
- 打开第一个CSV和每一行追加到一个列表
- 剪辑上三行(有一些无用的列标题的信息,我不希望)
- 存储这些结果的列表,我所谓的“存档
- 打开下一个CSV文件,重复(夹并添加EM为“档案”)
- 当我们在外面CSV文件我想写完整的“存档”在单独的文件夹中的文件。
因此,举例来说,如果我是开始与看起来像这三个CSV文件。
CSV 1
[]
[['Title'],['Date'],['etc']]
[]
[['Spam'],['01/01/2013'],['Spam is the spammiest spam']]
[['Ham'],['01/04/2013'],['ham is ok']]
[['Lamb'],['04/01/2013'],['Welsh like lamb']]
[['Sam'],['01/12/2013'],["Sam doesn't taste as good and the last three"]]
CSV 2
[]
[['Title'],['Date'],['etc']]
[]
[['Dolphin'],['01/01/2013'],['People might get angry if you eat it']]
[['Bear'],['01/04/2013'],['Best of Luck']]
CSV 3
[]
[['Title'],['Date'],['etc']]
[]
[['Spinach'],['04/01/2013'],['Spinach has lots of iron']]
[['Melon'],['02/06/2013'],['Not a big fan of melon']]
在其结束时我倒是家里得到的东西像...
CSV输出
[['Spam'],['01/01/2013'],['Spam is the spammiest spam']]
[['Ham'],['01/04/2013'],['ham is ok']]
[['Lamb'],['04/01/2013'],['Welsh like lamb']]
[['Sam'],['01/12/2013'],["Sam doesn't taste as good and the last three"]]
[['Dolphin'],['01/01/2013'],['People might get angry if you eat it']]
[['Bear'],['01/04/2013'],['Best of Luck']]
[['Spinach'],['04/01/2013'],['Spinach has lots of iron']]
[['Melon'],['02/06/2013'],['Not a big fan of melon']]
所以......我开始写这本:
import os
import csv
path = './Path/further/into/file/structure'
directory_list = os.listdir(path)
directory_list.sort()
archive = []
for file_name in directory_list:
temp_storage = []
path_to = path + '/' + file_name
file_data = open(path_to, 'r')
file_CSV = csv.reader(file_data)
for row in file_CSV:
temp_storage.append(row)
for row in temp_storage[3:-1]:
archive.append(row)
archive_file = open("./Path/elsewhere/in/file/structure/archive.csv", 'wb')
wr = csv.writer(archive_file)
for row in range(len(archive)):
lastrow = row
wr.writerow(archive[row])
print row
这似乎是工作...除了当我检查我的输出文件时,它似乎已经停止在接近尾声时怪点写”
例如:
[['Spam'],['01/01/2013'],['Spam is the spammiest spam']]
[['Ham'],['01/04/2013'],['ham is ok']]
[['Lamb'],['04/01/2013'],['Welsh like lamb']]
[['Sam'],['01/12/2013'],['Sam doesn't taste as good and the last three']]
[['Dolphin],['01/01/2013'],['People might get angry if you eat it']]
[['Bear'],['01/04/2013'],['Best of Luck']]
[['Spinach'],['04/0
这真是奇怪,什么出了错我不能工作了。 似乎是写罚款,但已经决定通过一个列表条目停止甚至一半。 跟踪回来,我肯定这事跟我上次写个“for循环”,但我不是太熟悉的CSV方法。 有有通过文档的读取,并且现在仍然难住了。
任何人都可以指出哪里我已经错了,我怎么可能会解决它,或许是否会有他的行动所有一个更好的办法!
非常感谢-Huw