简体   繁体   English

在Python中将从文件读取的字符串值转换为ASCII

[英]Convert string value read from file to ASCII in python

I will describe my problem in details: 我将详细描述我的问题:

First I'm reading some values from a CSV file as: 首先,我从CSV文件读取一些值,如下所示:

def import_csv(my_csv_file):
  with open(my_file, mode='r') as infile:
    reader = csv.reader(infile)
    next(reader) # Ignore the first row
    return dict((rows[0]+struct.pack('!L',int(rows[1])), [rows[2],rows[3]]) for rows in reader)

Then I call this function as: 然后我将此函数称为:

DB = import_csv(my_csv_file)

Each line in my_csv_file has the format: my_csv_file中的每一行具有以下格式:

40.1.1.2,1,\\x00123,bbbbbbbbbbbbbbbb 40.1.1.2,1,\\ x00123,bbbbbbbbbbbbbbbbbb

After importing I get one element of the dict as: 导入后,我得到该字典的一个元素为:

value = DB['40.1.1.2\x00\x00\x00\x01'][0]

But now I need to convert value (which contains the string \\x00123) to its ASCII equivalent where any two characters prefixed with \\x represent a HEX value. 但是现在我需要将 (包含字符串\\ x00123)转换为其等效的ASCII码,其中任何以\\ x开头的两个字符都代表一个十六进制值。 That is, for the example above, I want 也就是说,对于上面的示例,我想要

\\x00123 = 00313233 \\ x00123 = 00313233

I have tried with the binascii.hexlify command but that gives me the ASCII equivalent of each character in the read string, ie: 我已经尝试过使用binascii.hexlify命令,但这给了我读取字符串中每个字符的ASCII等效项,即:

binascii.hexlify(value) = 5c783030313233 binascii.hexlify(value)= 5c783030313233

If instead I do: 相反,如果我这样做:

value_0 = '\x00123'
print binascii.hexlify(value_0)

I get the desired result, ie, 00313233. So it seems that after reading the value from the file, the hex escape character \\x is considered as values in the string. 我得到了所需的结果,即00313233。因此,似乎从文件中读取了值之后,十六进制转义符\\ x被视为字符串中的值。 Note that the point of including eg the \\x00 part is to include non-printable characters in the value I will read from the file. 请注意,包括\\ x00部分的要点是在我将从文件读取的值中包括不可打印的字符。

Is there any way to do what I want? 有什么办法可以做我想要的吗? Thanks! 谢谢!

Why not add the conversion when creating the dictionary? 为什么在创建字典时不添加转换? Use decode('string_escape') to process any \\x , and then .encode('hex') to convert it back into hex: 使用decode('string_escape')处理任何\\x ,然后使用.encode('hex')将其转换回十六进制:

import csv
import struct

def import_csv(my_csv_file):
  with open(my_csv_file, mode='rb') as infile:
    reader = csv.reader(infile)
    next(reader) # Ignore the first row
    return dict((row[0] + struct.pack('!L', int(row[1])), [row[2].decode('string_escape').encode('hex'), row[3]]) for row in reader)

DB = import_csv('input.csv')

value = DB['40.1.1.2\x00\x00\x00\x01'][0]
print value

This would display: 这将显示:

00313233

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM