Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
update
  • Loading branch information
x-xz committed Sep 6, 2015
commit d2851709192b9c24f04d2dfbc349557778f5542d
8 changes: 5 additions & 3 deletions chapter5/2-createCsv.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import csv
#from os import open
import os

csvFile = open("../files/test.csv", 'w+')
filename = "../files/test.csv"
os.makedirs(os.path.dirname(filename), exist_ok=True)
csvFile = open(filename, 'w', newline='')
try:
writer = csv.writer(csvFile)
writer.writerow(('number', 'number plus 2', 'number times 2'))
for i in range(10):
writer.writerow( (i, i+2, i*2))
writer.writerow((i, i+2, i*2))
finally:
csvFile.close()
18 changes: 9 additions & 9 deletions chapter5/3-scrapeCsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

html = urlopen("http://en.wikipedia.org/wiki/Comparison_of_text_editors")
bsObj = BeautifulSoup(html)
#The main comparison table is currently the first table on the page
table = bsObj.findAll("table",{"class":"wikitable"})[0]
# The main comparison table is currently the first table on the page
table = bsObj.findAll("table", {"class": "wikitable"})[0]
rows = table.findAll("tr")

csvFile = open("files/editors.csv", 'wt')
csvFile = open("../files/editors.csv", 'w', encoding='utf-8', newline='')
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)
for row in rows:
csvRow = []
for cell in row.findAll(['td', 'th']):
csvRow.append(cell.get_text())
writer.writerow(csvRow)
finally:
csvFile.close()
csvFile.close()