简体   繁体   English

Pycharm和UTF-8代码

[英]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 在PyCharm中,出现以下错误

  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? PyCharm是否无法在文件中支持UTF-8? Or am I missing a setting somewhere. 还是我在某处缺少设置。 The same file builds in Sublime. 同一文件在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. 我之前在PyCharm中也遇到过类似的错误,我认为这与选择“ ascii”而不是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. 编码选项将强制文件加载UTF8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM