python文件读取不会读取许多表情符号字符

时间:2017-06-10 08:59:52

标签: python unicode emoji python-unicode

我在json文件中有一个1500 emoji字符字典的列表,我想将它们导入我的python代码,我做了一个文件读取并将其转换为python字典,但现在我只有143条记录。如何将所有表情符号导入我的代码,这是我的代码。

import sys
import ast

file = open('emojidescription.json','r').read()
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
emoji_dictionary = ast.literal_eval(file.translate(non_bmp_map))

#word = word.replaceAll(",", " ");

keys = list(emoji_dictionary["emojis"][0].keys())
values = list(emoji_dictionary["emojis"][0].values())

file_write = open('output.txt','a')

print(len(keys))
for i in range(len(keys)):
    try:
        content = 'word = word.replace("{0}", "{1}")'.format(keys[i],values[i][0])
    except Exception as e:
        content = 'word = word.replace("{0}", "{1}")'.format(keys[i],'')
    #file.write()
    #print(keys[i],values[i])
    print(content)


file_write.close()

这是我的输入样本

{

    "emojis": [
        {

            "‍": ["Graduate"],
            "©": ["Copy right"],
            "®": ["Registered"],
            "‍‍": ["family"],
            "‍❤️‍‍": ["love"],
            "™": ["trademark"],
            "‍❤‍": ["love"], 
            "⌚": ["time"],
            "⌛": ["wait"], 
            "⭐": ["star"],
            "": ["Elephant"],
            "": ["Cat"],
            "": ["ant"],
            "": ["cock"],
            "": ["cock"],

这是我的结果,143表示表情符号的数量。

  

143

     

word = word.replace(“ ”,“family”)

     

word = word.replace(“Ⓜ”,“”)

     

word = word.replace(“♥”,“”)

     

word = word.replace(“♠”,“”)

     

word = word.replace(“⌛”,“等待”)

2 个答案:

答案 0 :(得分:3)

我不确定为什么你只能看到输入1500的143条记录(你的样本似乎没有显示出这种行为)。

设置似乎没有做任何有用的事情,但你所做的事情归结为(简化和跳过大量细节):

d = ..read json as python dict.
keys = d.keys()
values = d.values()
for i in range(len(keys)):
    key = keys[i]
    value = values[i]

这应该是完全正确的。但是,有更好的方法可以在Python中执行此操作,例如使用zip函数:

d = ..read json as python dict.
keys = d.keys()
values = d.values()
for key, value in zip(keys, values):  # zip picks pair-wise elements
    ...

或只是询问字典的项目:

for key, value in d.items():
    ...

json模块使得读取和写入json更简单(更安全),并且使用上面的习语来解决问题:

import json

emojis = json.load(open('emoji.json', 'rb'))

with open('output.py', 'wb') as fp:
    for k,v in emojis['emojis'][0].items():
        val = u'word = word.replace("{0}", "{1}")\n'.format(k, v[0] if v else "")
        fp.write(val.encode('u8'))

答案 1 :(得分:1)

为什么用行中的0xfffd替换所有表情符号:

non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
emoji_dictionary = ast.literal_eval(file.translate(non_bmp_map))

请不要这样做!

使用json:

import json

with open('emojidescription.json', encoding="utf8") as emojis:
    emojis = json.load(emojis)

with open('output.txt','a', encoding="utf8") as output:
    for emoji, text in emojis["emojis"][0].items():
        text = "" if not text else text[0]
        output.write('word = word.replace("{0}", "{1}")\n'.format(emoji, text))