将输出写入csv文件[格式正确]

时间:2015-11-12 01:50:12

标签: python python-3.x

我意识到这个问题已被问过一百万次了,并且有很多关于它的文档。但是,我无法以正确的格式输出结果。

以下代码来自:Replacing empty csv column values with a zero

DataTemplate

目前,要获得正确的输出.csv文件[即以正确的格式]可以在bash中运行以下命令:

# Save below script as RepEmptyCells.py 
# Add #!/usr/bin/python to script 
# Make executable by chmod +x prior to running the script on desired .csv file 

# Below code will look through your .csv file and replace empty spaces with 0s
# This can be particularly useful for genetic distance matrices 

import csv
import sys

reader = csv.reader(open(sys.argv[1], "rb"))
for row in reader:
    for i, x in enumerate(row):
                if len(x)< 1:
                         x = row[i] = 0
    print(','.join(int(x) for x in row))

我尝试使用 #After making the script executable ./RepEmptyCells.py input.csv > output.csv # this produces the correct output 函数生成格式正确的csv.writer文件(类似于output.csv)而没有太多运气。

我想学习如何将最后一部分添加到代码中以自动执行该过程,而无需在bash中执行此操作。

我尝试过:

./RepEmptyCells.py input.csv > output.csv

当查看此代码和之前的原始文件时,它们看起来是一样的。

但是,当我在excel或iNumbers中打开它们时,后者(即f = open(output2.csv, 'w') import csv import sys reader = csv.reader(open(sys.argv[1], "rb")) for row in reader: for i, x in enumerate(row): if len(x)< 1: x = row[i] = 0 f.write(','.join(int(x) for x in row)) f.close() )只显示一行数据。

重要的是,output2.csvoutput.csv都可以在Excel中打开。

3 个答案:

答案 0 :(得分:3)

2个选项:

  1. 在您当前的f.write('\n')声明后进行f.write

  2. 使用csv.writer。你提到它但它不在你的代码中。

    writer = csv.writer(f)
    ...
    writer.writerow([int(x) for x in row])  # Note difference in parameter format
    

答案 1 :(得分:1)

一个不起眼的命题

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import sys

# Use with statement to properly close files
# Use newline='' which is the right option for Python 3.x
with open(sys.argv[1], 'r', newline='') as fin, open(sys.argv[2], 'w', newline='') as fout:
    reader = csv.reader(fin)
    # You may need to redefine the dialect for some version of Excel that 
    # split cells on semicolons (for _Comma_ Separated Values, yes...)
    writer = csv.writer(fout, dialect="excel")
    for row in reader:
        # Write as reading, let the OS do the caching alone
        # Process the data as it comes in a generator, checking all cells
        # in a row. If cell is empty, the or will return "0"
        # Keep strings all the time: if it's not an int it would fail
        # Converting to int will force the writer to convert it back to str
        # anwway, and Excel doesn't make any difference when loading.
        writer.writerow( cell or "0" for cell in row )

示例in.csv

1,2,3,,4,5,6,
7,,8,,9,,10

输出out.csv

1,2,3,0,4,5,6,0
7,0,8,0,9,0,10

答案 2 :(得分:0)

import csv
import sys

with open(sys.argv[1], 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row.replace(' ', '0')

并且我不了解您使用shell和重定向的需要。 一个csv作家只是:

with open('output.csv', 'wb') as f:
    writer = csv.writer(f)
    writer.writerows(rows)