Issue
I tried this code:
import urllib2
from bs4 import BeautifulSoup
import csv
#Use request for tokens
import requests
import json
def write_csv2(filename, table):
with open(filename, 'w', newline = '') as csvfile:
out_csv = csv.writer(csvfile)
out_csv.writerows(table)
but I get an exception saying that 'newline' is an invalid keyword. Does this not work in Python 2.7? How can I specify the newline character when opening a file in text mode?
Solution
In Python 2.x, the io
standard library module provides the same interface for accessing files and streams that is the default in Python 3.x. Try using io.open()
, which supports the newline
keyword argument:
>>> import io
>>> with io.open(filename, 'w', newline='') as csvfile:
... out_csv = csv.writer(csvfile)
... out_csv.writerows(table)
Answered By - Ozgur Vatansever
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.