简体   繁体   中英

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:

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:

40.1.1.2,1,\\x00123,bbbbbbbbbbbbbbbb

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. That is, for the example above, I want

\\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(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. 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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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