I am trying to read the first column of my CSV, run a web-service using this column, take the output from this and append it to my CSV. I'd like to do this on a line-by-line basis.

Here is what I have come up with so far :

loadData = lambda f: np.genfromtxt(open(f,'r'), delimiter='\n')
with open('FinalCSV.csv','rb') as tsvin, open('FinalCSV.csv', 'a+b') as csvout:
    tsvin = list(np.array(p.read_table('train.tsv'))[:,0])
    writer = csv.writer(csvout)
    count = 0
    for row in csvout:
        sep = '|'
        row = row.split(sep, 1)[0]
        cmd = subprocess.Popen("python GetJustAlexaRanking.py " + row ,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           shell=True)
        (output, err) = cmd.communicate()
        exit_code = cmd.wait()
        outlist = output.split('\r\n')
        try:
          outrank1 = outlist[1][outlist[1].index(':')+1:]
        except ValueError:
          outrank1 = "?"
        row.append(str(outrank1).rstrip()) #writing,error here 
        print [str(outlist[0]).rstrip(), str(outrank1).rstrip()]
        count+=1

However this is giving me the error that

Traceback (most recent call last):
  File "File.py", line 28, in <module>
    row.append(str(outrank1).rstrip()) #writing,error here
AttributeError: 'str' object has no attribute 'append'

How can I accomplish what I wish to do?

Edit :

loadData = lambda f: np.genfromtxt(open(f,'r'), delimiter='\n')
with open('FinalCSV.csv','rb') as tsvread, open('FinalCSVFin.csv', 'wb') as csvout:
    tsvin = list(np.array(p.read_table('train.tsv'))[:,0])
    writer = csv.writer(csvout)
    count = 0
    for row in tsvread:
        sep = '|'
        row = row.split(sep, 1)[0]
        cmd = subprocess.Popen("python GetJustAlexaRanking.py " + row ,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           shell=True)
        (output, err) = cmd.communicate()
        exit_code = cmd.wait()
        outlist = output.split('\r\n')
        try:
          outrank1 = outlist[1][outlist[1].index(':')+1:]
        except ValueError:
          outrank1 = "?"
        row = [row, outrank1.rstrip()]
        writer.writerow(row)
        print [str(outlist[0]).rstrip(), str(outrank1).rstrip()]
        count+=1
有帮助吗?

解决方案

Your row is not a list, but a string:

row = row.split(sep, 1)[0]

You then use that string in a subprocess command.

You'll need to make it a list again; instead of append, use:

row = [row, outrank1.rstrip()]

where outrank1 is always a string anyway, no need to call str() on it.

Note that if you are trying to both read from and write to the csvout file handle, you'll have to be very careful about your read-write position. You cannot just write to a file handle and hope to replace existing data for example. Best to use a separate, new file to write to and have that replace the old file location by moving one over the other.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top