0
내 CSV의 첫 번째 열을 읽으려고합니다.이 열을 사용하여 웹 서비스를 실행하고이 출력을 가져 와서 내 CSV에 추가하려고합니다. 저는 이것을 줄 단위로하고 싶습니다. 여기즉시 CSV에 열을 추가하는 방법
내가 지금까지 함께 온 것입니다 :
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
그러나이 나에게
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'
가 어떻게 내가 원하는 것을 달성 할 수있는 오류를주고있다?
편집 :
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
응답 해 주셔서 대단히 감사드립니다. 위의 코드를 편집하여 현재 변경 사항을 실행하고 있는지를 보여줍니다. 이 코드는 이제 실행되지만 출력에 열을 추가하지는 않습니다 (사실 출력 파일에는 아무런 차이가 없습니다). 어떤 제안? 고마워요 :) –
@ SimonKiely : 파일에 행을 쓰지 않고 .. –
죄송합니다, 나는 거기에 잘못된 텍스트를 복사했습니다! 나는 그것을 지금 새롭게했다. 현재 실행 중이지만 결국 IndexError로 인해 실패합니다. 또한 내 출력을 잘못된 형식으로 인쇄합니다 (http://i.imgur.com/borLltB.png). 도와 주셔서 대단히 감사합니다! :) –