[英]How to read unicode from text file and write respective string into exel file by using Python
I have a file which contains Unicode characters of Japanese language in a file and I would like to read the Unicode from the file and write the respective non-Unicode character (string) into some another file. 我有一个文件,其中包含日语中的Unicode字符,我想从文件中读取Unicode并将相应的非Unicode字符(字符串)写入另一个文件。
The Unicode in the file is like this: 文件中的Unicode如下所示:
\u6C0F\u540D \u7BA1\u7406\u8005\u540D \u4F4F\u6240 \u96FB\u8A71\u756A\u53F7 \u30E1\u30FC\u30EB\u30A2\u30C9\u30EC\u30B9
Actually, I want to generate an Excel file from this unicode, which consists of the non-Unicode characters of the mentioned unicode. 实际上,我想从此Unicode生成一个Excel文件,该文件由上述Unicode的非Unicode字符组成。
If you have a file called japanese.txt
with the following contents: 如果您有一个名为japanese.txt
的文件,其内容如下:
\u6C0F\u540D
\u7BA1\u7406\u8005\u540D
\u4F4F\u6240
\u96FB\u8A71\u756A\u53F7
\u30E1\u30FC\u30EB\u30A2\u30C9\u30EC\u30B9
You could add it to an Excel file with openpyxl
, using the following code: 您可以使用以下代码使用openpyxl
将其添加到Excel文件中:
# -*- coding: utf-8 -*-
from openpyxl import Workbook
import codecs
with codecs.open('japanese.txt', 'r', encoding='utf8') as file:
s = file.read()
s = s.decode('unicode-escape')
wb = Workbook()
ws = wb.active
ws['A1'] = 42
ws.append([1, 2, 3])
import datetime
ws['A2'] = s
wb.save("sample.xlsx")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.