无法清除十六进制字符

2024-04-25 17:27:50 发布

您现在位置: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)
   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()返回一个错误(即使在导入编解码器之后)。你知道吗


Tags: innewfordatalentablerangenum
1条回答
网友
1楼 · 发布于 2024-04-25 17:27:50

您可以使用parse,这是一个python模块,允许您在字符串中搜索规则格式的组件,并且可以从返回的组件中提取相应的整数,从原始字符串中替换它们。你知道吗

例如(未测试警报!)地址:

import parse

# Parse all hex-like items
list_of_findings = parse.findall("\\x{:w}", your_string)

# For each item
for hex_item in list_of_findings:

    # Replace the item in the string
    your_string = your_string.replace(
        # Retrieve the value from the Parse Data Format
        hex_item[0],
        # Convert the value parsed to a normal hex string,
        # then to int, then to string again
        str(int("0x"+hex_item[0]))
    )

Obs:您可以使用chr,而不是“int”,将找到的十六进制值转换为字符,如下所示:

chr(hex_item[0])

相关问题 更多 >

    热门问题