문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top