以UTF-16编码方式写入Excel字符串 [英] Writing to excel string in encoding UTF-16

查看:254
本文介绍了以UTF-16编码方式写入Excel字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以encoding UTF-16模式打开文本文件:

I'm opening text file in encoding UTF-16 mode:

with open(file.txt, 'r', encoding="UTF-16") as infile:

然后我要写入一个Excel文件:

Then I want to write to an excel file:

from csv import writer
excelFile = open("excelFile_1.csv", 'w', newline='') 
write = writer(excelFile, delimiter=',')
write.writerows([[input]])

其中input是文本文件file.txt

我收到以下错误

UnicodeEncodeError: 'charmap' codec can't encode character '\xe9' in position 113: character maps to <undefined>

使用Python 3.2

Using Python 3.2

推荐答案

您还需要选择CSV文件的输出编码:

You need to pick an output encoding for the CSV file as well:

excelFile = open("excelFile_1.csv", 'w', newline='', encoding='UTF16') 

系统的默认编解码器无法处理从输入文件名读取的代码点.

The default codec for your system cannot handle the codepoints you are reading from the input filename.

在Excel中打开此文件 可能不起作用;请按照此答案中的步骤进行操作,选择UTF16编解码器,以确保Excel正确读取文件.

Opening this file in Excel may not work; do follow the procedure in this answer, picking the UTF16 codec, to ensure that Excel reads the file correctly.

您还可以尝试使用UTF-8,将UTF-8 BOM添加到文件的开头:

You could also try using UTF-8, adding in a UTF-8 BOM to the start of the file:

excelFile = open("excelFile_1.csv", 'w', newline='', encoding='UTF8')
excelFile.write('\ufeff')  # Zero-width non-breaking space, the Byte Order Mark

主要是Microsoft软件,它在UTF-8文件中使用BOM,因为与UTF-16和UTF-32不同,UTF-8仅具有一个字节顺序可供选择,但显然使Excel变得更快乐.

It is mostly Microsoft software that uses a BOM in UTF-8 files, since UTF-8 only has one byte order to pick from, unlike UTF-16 and UTF-32, but it apparently makes Excel happy(er).

这篇关于以UTF-16编码方式写入Excel字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆