gpt4 book ai didi

python - CSV.writerow 每个字符之间有逗号?

转载 作者:太空宇宙 更新时间:2023-11-04 10:38:47 24 4
gpt4 key购买 nike

我目前正在我的 Python 脚本中调用一个 python 脚本,并试图将我的调用输出保存在一个 CSV 文件中。目前它可以工作,但是,每个字符之间都有一个逗号,因此输出不正确。

这是什么原因造成的?

import csv
import GetAlexRanking #External Method exposed here
import subprocess
import pandas as p
import tai
import numpy as np

loadData = lambda f: np.genfromtxt(open(f,'r'), delimiter=' ')
with open('train.tsv','rb') as tsvin, open('PageRanks.csv', 'wb') as csvout:
tsvin = list(np.array(p.read_table('train.tsv'))[:,0])
csvout = csv.writer(csvout)

for row in tsvin:
count = 0
cmd = subprocess.Popen("python GetAlexRanking.py " + row ,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
(output, err) = cmd.communicate()
exit_code = cmd.wait()
print exit_code #testing
print output
print err
csvout.writerow(row + "\t" + output) #writing,error here
count+=1

编辑:

在 cmd 中调用时从函数返回的示例行 "python GetAlexRanking.py www.google.com" 是:

www.google.com
AlexaTrafficRank:1
GooglePageRank:9

我希望将其保存在 tsv 中(添加空格以使格式更清晰,所有列仅由制表符分隔 :))

URL \t AlexaRank \t GoogleRank
www.google.com \t 1 \t 9

最佳答案

您正在将一个字符串传递给 csv.write,然后它将其解释为一个列表,因此将它按每个列表元素(即字符)拆分。这个错误我已经犯过很多次了...

试试这个:

# add coustom code to split the row up into the values, hint user row.split()
csvout.writerow([row, output])

关于python - CSV.writerow 每个字符之间有逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22075963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com