I am trying to fetch data from the internet to save it to a csv file.
I am facing a problem with writing to the csv file, the library leaves an empty row in the file
The data is random.org integers in text/plain format.
I'm using urllib.request to fetch the data and I am using this code to get the data and decode it
req = urllib.request.Request(url, data=None, headers={
'User-Agent': '(some text)'})
with urllib.request.urlopen(req) as response:
html = response.read()
encoding = response.headers.get_content_charset('utf-8')
data = html.decode(encoding)
I am using this line of code to open the csv file :csvfile = open('data.csv', "a")
Writing to the file:
writer = csv.writer(csvfile, lineterminator = '\n')
writer.writerows(data)
and of course I close the file at the end
Things I tried and didn't help :
- Using (lineterminator = '\n') when writing
- Using (newline = "") when opening the file
- Defining a "delimiter" and a "quotechar" and "quoting"