简体   繁体   English

如何使用Python从文本文件读取unicode并将相应的字符串写入exel文件

[英]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")

It appears that there is a package that can work for you called unidecode . 似乎有一个名为unidecode的软件包可以为您服务 It would do this very easily. 它将很容易做到这一点。 For instance: 例如:

>>> from unidecode import unidecode
>>> print(unidecode(u"\u6C0F\u540D"))
Shi Ming

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

相关问题 在Python中从文件读取和写入unicode - Read and write unicode from file in Python 如何在 Python 中从二进制文件读取和写入“Unicode(UTF-16 little endian)”文本? - How to read and write "Unicode (UTF-16 little endian)" text to and from a binary file in Python? 如何在 python 中写入和读取 unicode 到 json 文件 - how to write and read unicode to json file in python 如何使用相同的代码将unicode文本写入python 2和3中的文件? - How to write unicode text to file in python 2 & 3 using same code? 如何避免从python中的文本文件读取的unicode字符串中出现换行符 - how to avoid linebreakers from unicode string read from text file in python 如何将Unicode文本写入Python文件对象 - How to write Unicode text into a Python file object 如何解码从 Python 中的文件读取的 unicode 字符串? - How to decode unicode string that is read from a file in Python? 如何从.INI文件中读取字符串,然后从文本文件中读取该字符串并使用Python打印整行? - How to read the string from an .INI file and then read that string from a text file and print the entire line using Python? 如何从 pdf 中提取文本到 Python 中 - How to extract text from pdf to exel in Python 如何使用 Python 从文件中读取文本? - How to read text from a file using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM