在Python中编写文本文件时从Unicode转换为ASCII的错误

时间:2015-10-29 18:23:07

标签: python unicode

我从web服务获取明文到对象p,我希望使用Python将其存储在.txt文件中。

我做的事情如下:

with open("output","w") as t2:
    t2.write(p.content)

但出现以下编码问题:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 10: ordinal not in range(128)

我在Ubuntu和PycharmIDE上。我怎样才能克服这个问题?

1 个答案:

答案 0 :(得分:3)

您可以尝试使用

<?php $ds = DIRECTORY_SEPARATOR; //1 $storeFolder = 'uploads'; if (!empty($_FILES)) { $tempFile = $_FILES['file']['tmp_name']; $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds; $targetFile = $targetPath. $_FILES['file']['name']; move_uploaded_file($tempFile,$targetFile); / } ?>

p2.content.encode('utf-8')