I made the Python code as below.
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("https://en.wikipedia.org/wiki/Comparison_of_text_editors")
bs0bj = BeautifulSoup(html, "html.parser")
table = bs0bj.findAll("table", {"class":"wikitable"})[0]
rows = table.findAll("tr")
csvFile = open("../files/editors.csv", 'wt')
writer = csv.writer(csvFile)
try:
for row in rows:
csvRow = [ ]
for cell in row.findAll(['td', 'th']):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
finally:
csvFile.close()
There's an error like this.
FileNotFoundError Traceback (most recent call last) in () 10 table = bs0bj.findAll("table", {"class":"wikitable"})[0] 11 rows = table.findAll("tr") ---> 12 csvFile = open("../files/editors.csv", 'wt') 13 writer = csv.writer(csvFile) 14
FileNotFoundError: [Errno 2] No such file or directory: '../files/editors.csv'
I understand that you can make a csv file on Python, so please help me where the problem is.
python csv
First of all, the error message is that the file could not be found.
I said files/editors.csv, but...See if you have the /files directory.
And when I tested it, the example above in Windows might have an encoding error.
Enrico Tröger
There is a string called, and in window-encoded cp949, Umroot (German letter with two dots) is an indescribable character.
Please test it with the example below. Simplified revisions to repeat statements or resource handling.
If you use with the text, it automatically organizes file resources.
The _csv.writer has a method called writerows that can work well on multiple lines at the same time.
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
import codecs
html = urlopen("https://en.wikipedia.org/wiki/Comparison_of_text_editors")
bs0bj = BeautifulSoup(html, "html.parser")
table = bs0bj.findAll("table", {"class":"wikitable"})[0]
rows = table.findAll("tr")
with codecs.open("editors.csv", 'w', 'utf-8') as csvFile:
writer = csv.writer(csvFile)
csvRows = map(lambda row:(cell.get_text() for cell in row.findAll(['td', 'th'])), rows)
writer.writerows(csvRows)
© 2025 OneMinuteCode. All rights reserved.