Writing to a CSV file: ValueError: I/O operation on closed file
Writing to a CSV file: ValueError: I/O operation on closed file
代码从卡片中抓取了一个单独的事件,但我一直收到此错误:
C:\Python27\python.exe C:/Users/James/PycharmProjects/scraperscrappy
/scrapy.py Traceback (most recent call last):
File "C:/Users/James/PycharmProjects/scraperscrappy/scrapy.py", line 31, in
writer.writerow(c) ValueError: I/O operation on closed file
我正在使用 pyCharm。
进程已完成,退出代码为 1
Python 即时创建 csv 文件,但我也尝试在项目中创建一个 csv 文件,但我仍然得到相同的 message.I 认为问题可能出在 zip in line 之后的字符串 31.Any 帮助表示赞赏。
import csv
from urllib import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.gbgb.org.uk/resultsRace.aspx?id=1793467")
bsObj = BeautifulSoup(html)
one = bsObj.findAll("li", {"class": "first essential fin"})
two = bsObj.findAll("li", {"class": "essential greyhound"})
three = bsObj.findAll("li", {"class": "timeDistance"})
four = bsObj.findAll("li", {"class": "trap"})
five = bsObj.findAll("li", {"class": "sp"})
six = bsObj.findAll("li", {"class": "timeSec"})
seven = bsObj.findAll("li", {"class": "essential trainer"})
eight = bsObj.findAll("li", {"class": "first essential comment"})
firstessentialfin = [a.getText().strip() for a in one]
essentialgreyhound = [b.getText().strip() for b in two]
timeDistance = [c.getText().strip() for c in three]
trap = [d.getText().strip() for d in four]
sp = [e.getText().strip() for e in five]
timeSec = [f.getText().strip() for f in six]
essentialtrainer = [g.getText().strip() for g in seven]
firstessentialcomment = [h.getText().strip() for h in eight]
with open('dogfile.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=",")
for c in zip(firstessentialfin, essentialgreyhound, timeDistance, trap,sp, timeSec, essentialtrainer,
firstessentialcomment):
writer.writerow(c)
问题出在缩进上。将 for 循环移动到 with
上下文管理器主体下:
with open('dogfile.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=",")
for c in zip(firstessentialfin, essentialgreyhound, timeDistance, trap,sp, timeSec, essentialtrainer, firstessentialcomment):
writer.writerow(c)
代码从卡片中抓取了一个单独的事件,但我一直收到此错误:
C:\Python27\python.exe C:/Users/James/PycharmProjects/scraperscrappy /scrapy.py Traceback (most recent call last): File "C:/Users/James/PycharmProjects/scraperscrappy/scrapy.py", line 31, in writer.writerow(c) ValueError: I/O operation on closed file
我正在使用 pyCharm。
进程已完成,退出代码为 1 Python 即时创建 csv 文件,但我也尝试在项目中创建一个 csv 文件,但我仍然得到相同的 message.I 认为问题可能出在 zip in line 之后的字符串 31.Any 帮助表示赞赏。
import csv
from urllib import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.gbgb.org.uk/resultsRace.aspx?id=1793467")
bsObj = BeautifulSoup(html)
one = bsObj.findAll("li", {"class": "first essential fin"})
two = bsObj.findAll("li", {"class": "essential greyhound"})
three = bsObj.findAll("li", {"class": "timeDistance"})
four = bsObj.findAll("li", {"class": "trap"})
five = bsObj.findAll("li", {"class": "sp"})
six = bsObj.findAll("li", {"class": "timeSec"})
seven = bsObj.findAll("li", {"class": "essential trainer"})
eight = bsObj.findAll("li", {"class": "first essential comment"})
firstessentialfin = [a.getText().strip() for a in one]
essentialgreyhound = [b.getText().strip() for b in two]
timeDistance = [c.getText().strip() for c in three]
trap = [d.getText().strip() for d in four]
sp = [e.getText().strip() for e in five]
timeSec = [f.getText().strip() for f in six]
essentialtrainer = [g.getText().strip() for g in seven]
firstessentialcomment = [h.getText().strip() for h in eight]
with open('dogfile.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=",")
for c in zip(firstessentialfin, essentialgreyhound, timeDistance, trap,sp, timeSec, essentialtrainer,
firstessentialcomment):
writer.writerow(c)
问题出在缩进上。将 for 循环移动到 with
上下文管理器主体下:
with open('dogfile.csv', 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=",")
for c in zip(firstessentialfin, essentialgreyhound, timeDistance, trap,sp, timeSec, essentialtrainer, firstessentialcomment):
writer.writerow(c)