Python Can';不要去掉十六进制字符
这个程序生成一个来自文本文件的动词数组Python Can';不要去掉十六进制字符,python,Python,这个程序生成一个来自文本文件的动词数组 file = open("Verbs.txt", "r") data = str(file.read()) table = eval(data) num_table = len(table) new_table = [] for x in range(0, num_table): newstr = table[x].replace(")", "") split = newstr.rsplit("(") numx = len(split)
file = open("Verbs.txt", "r")
data = str(file.read())
table = eval(data)
num_table = len(table)
new_table = []
for x in range(0, num_table):
newstr = table[x].replace(")", "")
split = newstr.rsplit("(")
numx = len(split)
for y in range(0, numx):
split[y] = split[y].split(",", 1)[0]
new_table.append(split[y])
num_new_table = len(new_table)
for z in range(0, num_new_table):
print(new_table[z])
但是,文本本身包含十六进制字符,例如
('a\\xc4\\x9fr\\xc4\\xb1[Verb]+[Pos]+[Imp]+[A2sg]', ':', 17.6044921875)('A\\xc4\\x9fr\\xc4\\xb1[Noun]+[Prop]+[A3sg]+[Pnon]+[Nom]', ':', 11.5615234375)
我正试图摆脱这些。我该怎么做
我到处都查过,decode()返回一个错误(即使在导入编解码器之后)。您可以使用一个python模块,它允许您在字符串中搜索规则格式的组件,并且可以从返回的组件中提取相应的整数,从原始字符串中替换它们
例如(未测试的警报!):
Obs:您可以使用chr
将找到的类似十六进制的值转换为字符,而不是“int”,如下所示:
chr(hex_item[0])
您能提供一个“verbs.txt”内容的示例吗?如果您将其解码为unicode,看起来您的动词是
ağrı
。因为这很可能是垃圾,所以您需要先知道写入文件时使用了什么编码,然后才能对其进行处理。此外,还需要说明如何使用decode以及它会产生什么错误。data=str(file.read())
充其量也没用-file.read()
返回一个str
已调用的eval()
不受信任的数据是一个巨大的安全问题,当然不是解析数据的正确方法,您可以直接在序列上迭代,无需使用“范围内的x(len(无论什么))”。也许你应该从摆脱这些废话开始?我应该如何解析我的数据?ImportError:没有名为“parse”的模块。“parse”不是Python默认库的一部分。您可以使用“pip install parse”安装它
chr(hex_item[0])