我知道这个问题已经被问了一百万次了,而且有很多相关的文档。但是,我无法以正确的格式输出结果。在# 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))
目前,要获得正确的输出.csv文件[即格式正确],可以在bash中运行以下命令:
^{pr2}$
我尝试过使用csv.writer函数来生成格式正确的output.csv文件(类似于./RepEmptyCells.py input.csv > output.csv),但没有多少运气。在
我想学习如何将最后一部分添加到代码中,以使过程自动化,而不必在bash中执行。在
我所做的:f = open(output2.csv, 'w&