wegry
wegry

Reputation: 8147

Pycharm and UTF-8 in code

When I try to run this code:

with open('scraped.csv', 'w') as dump:
        writer = csv.writer(dump, delimiter='ę', escapechar='\\',quoting=csv.QUOTE_NONE)
        writer.writerow(('author', 'ups', 'downs',
                         'score', 'sub_name', 'replys',
                         'created_utc', 'pulled down at'))

in PyCharm, I get the following error

  File ..., line 38, in <module>
'created_utc', 'pulled down at'))
UnicodeEncodeError: 'ascii' codec can't encode character '\u0119' in position 6: ordinal not in range(128)

Is PyCharm not able to support UTF-8 in files? Or am I missing a setting somewhere. The same file builds in Sublime.

Upvotes: 2

Views: 5857

Answers (1)

Tim
Tim

Reputation: 2637

I've had a similar error before in PyCharm, I think it is something to do with the encoding detection selecting 'ascii' rather than UTF8. There is however a simple work around:

with open('scraped.csv', 'w', encoding='utf8') as dump:
    writer = csv.writ...

The encoding option will force the file to load UTF8.

Upvotes: 5

Related Questions