从列表写入CSV,write.row似乎停在一个陌生的地方

Huw*_*Huw 2 python csv list output

我正在尝试合并许多CSV文件.我的初始功能旨在:

  • 查看目录内部并计算其中的文件数(假设所有文件都是.csv)
  • 打开第一个CSV并将每行附加到列表中
  • 剪辑前三行(我不想要一些无用的列标题信息)
  • 将这些结果存储在我称为"存档"的列表中
  • 打开下一个CSV文件并重复(剪辑并将em附加到'archive')
  • 当我们没有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"]]
Run Code Online (Sandbox Code Playgroud)

CSV 2

[]
[['Title'],['Date'],['etc']]
[]
[['Dolphin'],['01/01/2013'],['People might get angry if you eat it']]
[['Bear'],['01/04/2013'],['Best of Luck']]
Run Code Online (Sandbox Code Playgroud)

CSV 3

[]
[['Title'],['Date'],['etc']]
[]
[['Spinach'],['04/01/2013'],['Spinach has lots of iron']]
[['Melon'],['02/06/2013'],['Not a big fan of melon']]
Run Code Online (Sandbox Code Playgroud)

最后,我回家得到类似的东西......

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']]
Run Code Online (Sandbox Code Playgroud)

所以...我开始写这个:

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
Run Code Online (Sandbox Code Playgroud)

这似乎工作...除非我检查我的输出文件,它似乎已停止写在一个接近结尾的奇怪点"

例如:

[['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
Run Code Online (Sandbox Code Playgroud)

它真的很奇怪,我无法弄清楚出了什么问题.似乎写得很好,但决定在列表条目的一半停止.追溯它我确定这与我上次写的"for循环"有关,但我对csv方法并不太熟悉.有一个阅读文档,我仍然难过.

任何人都可以指出我出错的地方,我可能会如何修复它,也许还有更好的办法解决这一切!

非常感谢-Huw

unu*_*tbu 8

在脚本结束之前关闭文件句柄.关闭文件句柄也会刷新任何等待写入的字符串.如果您不刷新并且脚本结束,则可能永远不会写入某些输出.

使用with open(...) as f语法很有用,因为当Python离开with-suite 时,它将为您关闭文件.有了with,你永远不会再省略关闭文件.

with open("./Path/elsewhere/in/file/structure/archive.csv", 'wb') as archive_file:
    wr = csv.writer(archive_file)
    for row in archive:
        wr.writerow(row)
    print row
Run Code Online (Sandbox Code Playgroud)