字符仅在行尾显示为菱形问号(Python> Text) [英] Character showing up as diamond question mark only at end of line (Python>Text)

查看:190
本文介绍了字符仅在行尾显示为菱形问号(Python> Text)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个Python文件,该文件输入其中包含日语字符(UTF-8)的文本文件,将其中一些文本写入到新的UTF-8文本文件中。

I'm working on a Python file that inputs a text file with Japanese characters (UTF-8) in it, takes some of the text, and writes it into a new UTF-8 text file.

我遇到的问题是,由于某种原因,每当日语字符だ出现在原始输入文件的一行的末尾时,它就会以输出文件中的菱形问号。

The problem I'm coming across is that for some reason whenever the Japanese character だ appears at the end of a line in the original input file, it comes out as a diamond question mark in the output file.

在行尾之前的Instance实例读得很好,即使在输入的末尾,原始输入文件也能读得很好。

Instances of だ before the end of a line read perfectly fine and the original input file has it reading perfectly fine even if it's at the end of the line.

推荐答案

由于您尚未共享任何代码段,因此我建议您使用一种通用的读写方式 utf-使用编解码器模块的8 个文件为:

As you haven't shared any code snippet I would recommend you a generic way of reading and writing utf-8 files using the codecs module as:

# Reading utf-8 encoded file
with codecs.open("in.txt", "r", encoding="utf-8") as input_data:
    data = input_data.read()

# Write utf-8 encoded file
with codecs.open("out.txt", "w", encoding="utf-8") as output_data:
     output_data.write(data)

顺便说一句,我在给定字符tested上对其进行了测试,并且效果很好。

And BTW I tested it on the given character だ and it works pretty fine.

这篇关于字符仅在行尾显示为菱形问号(Python> Text)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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