Search csv for hex data using python

I have a csv file that has 3 columns. I am trying to search the second column for a specific value (hexadecimal values) and read the next record in this row (column 3). The format is similar to the one below:

Text1, 0x04d0a053, value1 Text2, 0x04d01053, value2 Text3, 0x04d03053, value3 Text4, 0x04d05053, value4 Text5, 0x04d00053, value5 Text6, 0x04d02053, value6 Text7, 0x04d04053, value7 Text8, 0x04413053, value8 

I have no problem finding and reading the last value (0x04413053) and printing "value8". However, when I try to search for any of the first 7 records, nothing is read ([] in the output). My code is below, who has an idea of ​​what might be a bug?

 fileInput = 'mycsv.csv' column0 = 0 column1 = 1 column2 = 2 #reads correctly hexvalue = hex(0x04413053) with open(fileInput, 'r') as file: reader = csv.reader(file) entry = [line[column2] for line in reader if line[column1] == hexvalue] print entry #does not read correctly hexvalue = hex(0x04d0a053) with open(fileInput, 'r') as file: reader = csv.reader(file) entry = [line[column2] for line in reader if line[column1] == hexvalue] print entry 
+4
source share
5 answers

hex (0x 0 4413053) - "0x4413053"

You should probably do the opposite, i.e.

 int(line[clolumn1], 16) == 0x04413053 
+7
source

In both cases, you read all the values ​​in a for statement, you shouldn't. Just do:

 for line in reader: if line[column1] == hexvalue: entry = line[column2] break # efficient! print entry 
+1
source

In addition to the type specified in MK fine answer , I noticed that the csv example you present has some problems with spaces that break your code. Here is my solution:

 fileInput = 'mycsv.csv' # Sniff csv format to easily avoid whitespace issue. with open(fileInput, 'r') as afile: # 'file' is a python keyword. Best to avoid overriding it. snift = csv.Sniffer().sniff(afile.readline()) # get the answers in a single pass at the file. # If that doesn't work for you, fine, but try to avoid opening the file twice. hexvalues = set([0x04413053, 0x04d0a053]) with open(fileInput, 'r') as afile: reader = csv.reader(afile, dialect=snift) entries = [line[2] for line in reader if int(line[1], 16) in hexvalues] print '\n'.join(entries) 
0
source

The problem is the if condition. You are not comparing the same values.

  hex_string = "0x04413053" with open(fileInput, 'r') as file: reader = csv.reader(file) entry = [line[column2] for line in reader if int(line[column1], 16) == int(hex_string, 16)] print entry 

The code above shows that you must compare the same types. This can be rewritten as:
(FYI: int(line[column1], 16) converts the string to hexadecimal)

  hexvalue = 0x04413053 with open(fileInput, 'r') as file: reader = csv.reader(file) entry = [line[column2] for line in reader if int(line[column1], 16) == hexvalue] print entry 
0
source

After playing with all the examples, I came to the working code below:

Anthon was right in that I just needed to sort the list once for the value, in my case there would be no duplicate patterns. I had to modify Anthon to add input from MK to find the hex value I was looking for.

Thanks for the help.

 hexvalue = 0x04d03053 with open(fileInput, 'r') as file: reader = csv.reader(file) for line in reader: if int(line[column1], 16) == hexvalue: entry = line[column2] break # efficient! print entry 
0
source

Source: https://habr.com/ru/post/1414451/


All Articles