【问题标题】:Writing to CSV from list, write.row seems to stop in a strange place从列表写入CSV,write.row似乎停在一个奇怪的地方
【发布时间】:2013-03-24 05:30:10
【问题描述】:

我正在尝试合并多个 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

【问题讨论】:

    标签: python list csv output


    【解决方案1】:

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

    使用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
    

    【讨论】:

    • 宾果游戏,非常感谢你是正确的,这是我缺乏关闭。已经重新编写它以使用它,现在在我停止使用它之后执行 variable.close() !谢谢!!!
    • 你能举个例子,在不使用with的情况下关闭csv编写器吗?我正在修改一个使用多个 ajax 请求写入 csv 的脚本,因此 csv 需要保持打开状态,直到所有请求都完成并被写入。我试过csv.close() 并得到'_csv.writer' object has no attribute 'close'。 csv 模块的文档没有说明关闭文件...
    • @Billy:在上面的例子中,如果文件是用archive_file = open(...)打开的,那么它可以用archive_file.close()关闭。
    猜你喜欢
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多