如何使用 Python 将文本文件中的 Unicode 字符替换为土耳其语字符

S.S*_*vaS 4 python unicode turkish replace character

我正在推特上工作。我使用 Stream API 从 Twitter 获取数据,应用程序的结果是 JSON 文件。我在文本文件中写入了推文数据,现在我看到的是 Unicode 字符而不是土耳其语字符。我不想在 Notepad++ 中手动查找/替换。是否有任何自动选项可以通过打开 txt 文件、读取文件中的所有数据并通过 Python 将 Unicode 字符更改为土耳其语字符来替换字符?

\n\n

以下是我要替换的 Unicode 字符和土耳其语字符。

\n\n
    \n
  • \xc4\x9f - \\u011f
  • \n
  • \xc4\x9e - \\u011e
  • \n
  • \xc4\xb1 - \\u0131
  • \n
  • \xc4\xb0 - \\u0130
  • \n
  • \xc3\xb6 - \\u00f6
  • \n
  • \xc3\x96 - \\u00d6
  • \n
  • \xc3\xbc - \\u00fc
  • \n
  • \xc3\x9c - \\u00dc
  • \n
  • \xc5\x9f - \\u015f
  • \n
  • \xc5\x9e - \\u015e
  • \n
  • \xc3\xa7 - \\u00e7
  • \n
  • \xc3\x87 - \\u00c7
  • \n
\n\n

我尝试了两种不同的类型

\n\n
#!/usr/bin/env python\n\n# -*- coding: utf-8 -*- \n\nimport re\n\ndosya = open(\'veri.txt\', \'r\')\n\nfor line in dosya:\n    match = re.search(line, "\\u011f")\n    if (match):\n        replace("\\u011f", "\xc4\x9f")\n\ndosya.close()\n
Run Code Online (Sandbox Code Playgroud)\n\n

和:

\n\n
#!/usr/bin/env python\n\n# -*- coding: utf-8 -*- \n\nf1 = open(\'veri.txt\', \'r\')\nf2 = open(\'veri2.txt\', \'w\')\n\nfor line in f1:\n    f2.write=(line.replace(\'\\u011f\', \'\xc4\x9f\')) \n    f2.write=(line.replace(\'\\u011e\', \'\xc4\x9e\'))\n    f2.write=(line.replace(\'\\u0131\', \'\xc4\xb1\'))\n    f2.write=(line.replace(\'\\u0130\', \'\xc4\xb0\'))\n    f2.write=(line.replace(\'\\u00f6\', \'\xc3\xb6\'))\n    f2.write=(line.replace(\'\\u00d6\', \'\xc3\x96\'))\n    f2.write=(line.replace(\'\\u00fc\', \'\xc3\xbc\'))\n    f2.write=(line.replace(\'\\u00dc\', \'\xc3\x9c\'))\n    f2.write=(line.replace(\'\\u015f\', \'\xc5\x9f\'))\n    f2.write=(line.replace(\'\\u015e\', \'\xc5\x9e\'))\n    f2.write=(line.replace(\'\\u00e7\', \'\xc3\xa7\'))\n    f2.write=(line.replace(\'\\u00c7\', \'\xc3\x87\'))\n\nf1.close()\nf2.close()\n
Run Code Online (Sandbox Code Playgroud)\n\n

这两个都不起作用。\n我怎样才能让它工作?

\n

Man*_*cob 6

JSON 允许“转义”和“未转义”字符。Twitter API 只返回转义字符的原因是它可以使用 ASCII 编码,从而提高了互操作性。对于土耳其语字符,您需要另一种编码。使用该函数打开文件open会打开一个假定您当前的区域设置编码的文件,这可能是您的编辑器所期望的。如果您希望输出文件具有例如编码ISO-8859-9,您可以将encoding='ISO-8859-9' 作为附加参数传递给函数open

您可以使用该函数读取包含 JSON 对象的文件json.load。这将返回一个 Python 对象,其中转义字符已解码。再次写入并作为参数json.dump传递,ensure_ascii=False会将对象写回文件,而不将土耳其字符编码为转义序列。一个例子:

import json
inp = open('input.txt', 'r')
out = open('output.txt', 'w')
in_as_obj = json.load(inp)
json.dump(in_as_obj, out, ensure_ascii=False)
Run Code Online (Sandbox Code Playgroud)

您的文件实际上并不是一个 JSON 文件,而是一个包含多个 JSON 对象的文件。如果每个 JSON 对象都在自己的行上,您可以尝试以下操作:

import json
inp = open('input.txt', 'r')
out = open('output.txt', 'w')
for line in inp:
    if not line.strip():
        out.write(line)
        continue
    in_as_obj = json.loads(line)
    json.dump(in_as_obj, out, ensure_ascii=False)
    out.write('\n')
Run Code Online (Sandbox Code Playgroud)

但就您而言,最好首先将未转义的 JSON 写入文件。尝试将您的on_data方法替换为(未经测试):

def on_data(self, raw_data):
    data = json.loads(raw_data)
    print(json.dumps(data, ensure_ascii=False))
Run Code Online (Sandbox Code Playgroud)


小智 5

你可以使用这个方法:

\n
# For Turkish Character\ntranslationTable = str.maketrans("\xc4\x9f\xc4\x9e\xc4\xb1\xc4\xb0\xc3\xb6\xc3\x96\xc3\xbc\xc3\x9c\xc5\x9f\xc5\x9e\xc3\xa7\xc3\x87", "gGiIoOuUsScC")\n\nyourText = "Pijamal\xc4\xb1 Hasta Ya\xc4\x9f\xc4\xb1z \xc5\x9eof\xc3\xb6re \xc3\x87abucak G\xc3\xbcvendi"\nyourText = yourText.translate(translationTable)\n\nprint(yourText)\n
Run Code Online (Sandbox Code Playgroud)\n