I'm trying to use csv writer in Python to write my output data to a file. When I just use the print command, the data looks good. But when I use the writerow
command (line 20), nothing goes into the file.
I know the code isn't the prettiest and may not be the most efficient, but it (almost) works for what I need.
Here's my code:
import requests
from BeautifulSoup import BeautifulSoup
import csv
symbols = {'AMZN', 'BAC', 'GOOG', 'RCL'}
with open('symbols.csv', "w") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for s in symbols:
try:
url1 ='https://research.tdameritrade.com/grid/public/research/stocks/fundamentals?symbol='
full_url = url1 + s
response = requests.get(full_url)
html = response.content
soup = BeautifulSoup(html)
for hist_div in soup.find("div", {"data-module-name": "HistoricGrowthAndShareDetailModule"}):
EPS = hist_div.find('label').text
print (s + ' ' + EPS) #this works and prints out good looking data
#writer.writerow([s,EPS])<<this doesn't print anything to file
except Exception as e:
continue
It makes sense that this is what you get. If you'll notice, at the time you call writer.writerow
, you've already closed the file. Well, you don't do it explicitly, but since you're using the with
context manager, the file is automatically closed once the with
block is exited, so any write operations will be on the closed file, which isn't possible.
If you want this to work, the loop (and everything inside it) will need to be placed inside the with
block (so, indented one level deeper).
with open('symbols.csv', "w") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for s in symbols:
... # call writer.writerow inside the block, while the file is open