简体   繁体   English

CSV.writerow在每个字符之间都有逗号吗?

[英]CSV.writerow has comma between every character?

I am currently calling a python script within my Python script and trying to save the output of my call in a CSV file. 我目前在我的Python脚本中调用一个python脚本,并尝试将我的调用输出保存在CSV文件中。 At the moment it will work, however, there is a comma between every character so the output is incorrect. 目前,它可以工作,但是每个字符之间都有一个逗号,因此输出不正确。

What is causing this? 是什么原因造成的?

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

Edit: 编辑:

A sample row returned from the function when called in cmd like so "python GetAlexRanking.py www.google.com" is : 像这样"python GetAlexRanking.py www.google.com"在cmd中调用时,从函数返回的示例行是:

www.google.com
AlexaTrafficRank:1
GooglePageRank:9

I would like this to be saved in a tsv as so (spaces added to make formatting more clear, all columns separated by a tab only :)) 我希望这样保存在tsv中(添加空格以使格式更清晰,所有列仅由制表符分隔:))

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

You are passing a string to the csv.write, which it then interprets as a list and therefore splits it by each list element, ie character. 您正在将一个字符串传递给csv.write,然后它将其解释为列表,并因此按每个列表元素(即字符)进行拆分。 I've made this mistake so many times... 我犯了很多次这个错误...

Try this: 尝试这个:

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

It looks as if you want your input to be a list as well as your output. 似乎您希望您的输入既是列表又是输出。 Thus, let your input stay a set of strings and split it into a list at each row. 因此,让您的输入保留一组字符串并将其拆分为每一行的列表。

Your sample row as returned is showing in three lines. 返回的示例行显示为三行。 Does this mean it is one long string with the column separators? 这是否意味着它是一个带有列分隔符的长字符串? If that is the case split the output and insert the tabs. 如果是这种情况,请分割输出并插入选项卡。

  outrow = row # row is already a list
  outrow.append(output.split('\t'))
  csvout.writerow(outrow)

Looking again at your sample, it appears that you want to output two tsv rows, one with the "header" and one with the "rank". 再次查看示例,您似乎想输出两个tsv行,其中一个带有“ header”,另一个带有“ rank”。 Thus (with extra lines for ease of reading) 因此(带有额外的行,以便于阅读)

outlist = output.split('\t')
outname1 = outlist[1][0:outlist[1].index(':')-1]
outname2 = outlist[2][0:outlist[2].index(':')-1]
outrank1 = outlist[1][outlist[1].index(':')+1:]
outrank2 = outlist[2][outlist[2].index(':')+1:]
outrow1 = ['URL', outname1, outname2]
outrow2 = [outlist[0], outrank1, outrank2]

You would then write the two output rows as you seem to have put in your sample output 然后,您应该像在示例输出中那样输入两个输出行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM