1

Running this code creates the UnicodeEncodeError below. Encoding is utf-8 but apparently Python ignores it. I'm using python 2.7.14 on Anaconda2.

try: aux = pd.read_csv('E:\Python_webscraping_Ahmd/news.csv') except: aux = pd.DataFrame(columns=list(news.columns)) aux.to_csv('E:\Python_webscraping_Ahmd/news.csv', encoding= 'utf-8', index=False) with open('E:\Python_webscraping_Ahmd/news.csv', 'a') as f: news.to_csv(f, header=False, index=False) 

I get error message

UnicodeEncodeErrorTraceback (most recent call last) <ipython-input-12-8835f23065f2> in <module>() 75 76 if __name__ == '__main__': ---> 77 getDailyNews() 78 <ipython-input-12-8835f23065f2> in getDailyNews() 69 70 with open('E:\Python_webscraping_Ahmd/news.csv', 'a') as f: ---> 71 news.to_csv(f, header=False, index=False) 72 73 cleanData('E:\Python_webscraping_Ahmd/news.csv') E:\Anaconda\lib\site-packages\pandas\core\frame.py in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote, escapechar, decimal) 1401 doublequote=doublequote, 1402 escapechar=escapechar, decimal=decimal) -> 1403 formatter.save() 1404 1405 if path_or_buf is None: E:\Anaconda\lib\site-packages\pandas\io\formats\format.py in save(self) 1590 self.writer = csv.writer(f, **writer_kwargs) 1591 -> 1592 self._save() 1593 1594 finally: E:\Anaconda\lib\site-packages\pandas\io\formats\format.py in _save(self) 1691 break 1692 -> 1693 self._save_chunk(start_i, end_i) 1694 1695 def _save_chunk(self, start_i, end_i): E:\Anaconda\lib\site-packages\pandas\io\formats\format.py in _save_chunk(self, start_i, end_i) 1717 quoting=self.quoting) 1718 -> 1719 lib.write_csv_rows(self.data, ix, self.nlevels, self.cols, self.writer) 1720 1721 pandas\_libs\lib.pyx in pandas._libs.lib.write_csv_rows() UnicodeEncodeError: 'ascii' codec can't encode character u'\u2014' in position 134: ordinal not in range(128) 
1

1 Answer 1

0

You need to also specify encoding while writing the csv file:

news.to_csv(f, header=False, index=False, encoding="utf-8") 
Sign up to request clarification or add additional context in comments.

1 Comment

@m.borhan please accept the answer if it solved your problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.