我试图纠正下面的代码,没有结果,为维基百科页面的每个属性(t,s,u,li,i)编写带有列的fileout cvs。
如何更改它以获得正确的输出?
import csv
import wikipedia
wikipedia.set_lang('it')
fileout=open('D:\\GIS\\Dati\\Vinca\\out.csv', 'w', encoding="utf8")
with open('D:\\GIS\\Dati\\Vinca\\specie_vinca_min.csv', 'rt', encoding="utf8") as f:
reader = csv.reader(f)
writer = csv.writer(fileout)
for row in reader:
try:
wikipage = wikipedia.page(row[0], auto_suggest=False)
t=wikipage.title
s=wikipage.summary
u=wikipage.url
li=len(wikipage.images)
i=wikipage.images[0]
tot=t, s, u, li, i
writer.writerow(tot)
except wikipedia.exceptions.PageError:
#if a "PageError" was raised, ignore it and continue to next link
continue
fileout.close()
f.close()
答案 0 :(得分:1)
我找到了解决方案。
Wikipedia.summary输出包含许多创建列问题的\ n。
添加st=s.replace('\n',' ')
,我删除了问题。
现在我有一个右列的表。
import csv
import wikipedia
wikipedia.set_lang('it')
fileout=open('D:\\GIS\\Dati\\Vinca\\out1.csv', 'w', encoding="utf8")
with open('D:\\GIS\\Dati\\Vinca\\specie_vinca_min.csv', 'rt', encoding="utf8") as f:
reader = csv.reader(f)
writer = csv.writer(fileout, delimiter='|')
for row in reader:
try:
wikipage = wikipedia.page(row[0], auto_suggest=False)
t=wikipage.title
s=wikipage.summary
st=s.replace('\n',' ')
u=wikipage.url
li=len(wikipage.images)
i=wikipage.images[0]
tot= t,st,u, li, i
writer.writerow(tot)
except wikipedia.exceptions.PageError:
#if a "PageError" was raised, ignore it and continue to next link
continue
fileout.close()
f.close()