Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python可以';t编码字符'\u3010';在位置0:字符映射到<;未定义>;_Python_Python 3.x - Fatal编程技术网

Python可以';t编码字符'\u3010';在位置0:字符映射到<;未定义>;

Python可以';t编码字符'\u3010';在位置0:字符映射到<;未定义>;,python,python-3.x,Python,Python 3.x,我正在尝试将一个python字符串列表与'\n'.join(self.product.features)连接成一个字符串,我可以将其保存到一个文件中。列表如下所示 [ "【SIX IN ONE】This digital radio alarm clock combines 6 functions: Digital clock 12/24 Hour Format for checking time; dual alarm clock with individual alarm volume c

我正在尝试将一个python字符串列表与
'\n'.join(self.product.features)
连接成一个字符串,我可以将其保存到一个文件中。列表如下所示

[
  "【SIX IN ONE】This digital radio alarm clock combines 6 functions: Digital clock 12/24 Hour Format for checking time; dual alarm clock with individual alarm volume control for awaking you up and you can adjust the alarm volume level; FM radio for listening to news& weather forecast; auto brightness & 3 steps dimmer control for eyes care; USB charging port for mobile device and easy to charge your phone near bedside; 3.5 mm jack (not included) for external audio source.",
  "【LARGE BRIGHT DISPLAY】Large 1.4-inch Cyan Blue LED display without any blink makes time easy to read from a far distance, auto brightness & 3 steps dimmer control for eyes caring, auto set the display to a brighter setting in daytime and softer one at night.",
  "【AUTO TIME SET】 Once you plugged this radio alarm clock to the AC Outlet, default EST time will be displayed. DST (Daylight Saving Time) will be switching automatically, Simple Time Zone Alignment (Press and hold SET button then adjust the Hour by Tune Up or Down), Backup Battery to maintain Time and Alarm Setting.",
  "【SUITABLE FOR HOME&OFFICE】You can place this radio alarm clock on bedside table; Office desk; kitchen; study table or cabinet at sitting room - Need to connect to main power.",
  "【30 DAYS MONEY BACK GUARANTEE】Please feel free to contact us if you have any questions on this radio alarm clock and you can buy with confidence at any time thanks to our 30-day money back guarantee."
]
这是我试图连接字符串并保存它的代码

txtfile = open(self.productDir + "\\Product Details.txt", "w+")
...
txtfile.write("\n\n")
if (self.product.features and len(self.product.features) > 0):
    txtfile.write('\n'.join(self.product.features))
else:
    txtfile.write('Has no features')
...
txtfile.close()
但我得到了错误

UnicodeEncodeError: 'charmap' codec can't encode character '\u3010' in position 0: character maps to <undefined>
UnicodeEncodeError:“charmap”编解码器无法对位置0中的字符“\u3010”进行编码:字符映射到

我可以看到一些字符无法解码,我只是不知道在这种情况下如何使用它来解码/编码或绕过它。

当您使用反斜杠(
\
)编写路径时,我假设您正在使用Windows。在Windows命令行界面(所谓的控制台)中,编码当前为cp1252(或其他8位编码)。字符U+3010(左黑柱状括号)不存在于cp1252字符集中(并且可能也不存在于Windows中任何常见的8位字符集中)

可能的解决办法:

  • 前后转换忽略不可映射字符-参数将消失

    charset = 'ascii'
    txt = '\n'.join(self.product.features).encode(charset, 'ignore').decode(charset)
    txtfile.write(txt)
    
  • 用合理的等效字符替换有问题的字符-参数将显示为
    ,但如果字符串中存在其他不可映射的字符,则参数将中断

    txt = '\n'.join(self.product.features).replace('\u3010', '(').replace('\u3011', ')')
    txtfile.write(txt)
    
如果您的系统使用字符集,请选择使用
cp1252
charset将两者结合起来:

    txt = '\n'.join(self.product.features).replace('\u3010', '(').replace('\u3011', ')')
    charset = 'cp1252'
    txtfile.write(txt.encode(charset, 'ignore').decode(charset))

只需在open函数中设置编码名称(例如
utf-8
),默认情况下python使用system encodingYes,它可以工作
txtfile=open(self.productDir+“\\Product Details.txt”,“w+”,encoding=“utf-8”)
我还建议将
结构一起使用,这与您的问题无关,但代码会更清晰。-@Harman的可能重复:原因与提议的重复不远,但解决方案可能不同。