Hoser
Hoser

Reputation: 87

Searching a csv for hex data with python

I have a csv file which has 3 columns. I am trying to search through the second column for a specific value (hex values) and read the next entry over in that line (column 3). The format is similar to the 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 searching and reading the last value (0x04413053) and printing 'value8'. However, when I attempt to search for any of the first 7 entries nothing is read back ([] on the output). My code is below, anyone have an idea of what the bug might be?

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

Upvotes: 4

Views: 2455

Answers (5)

Hoser
Hoser

Reputation: 87

After playing around with all of the examples I arrived at the below working code:

Anthon was correct in that I only needed to sort through the list once for the value, in my case there will be no repeating patterns. I did have to modify Anthon to add the input from MK to find the hex value I was searching 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

Upvotes: 0

varunl
varunl

Reputation: 20249

The problem is in 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 above code shows you should compare the same types. This can be rewritten as:
(FYI: int(line[column1], 16) converts the string to hex)

  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

Upvotes: 0

kojiro
kojiro

Reputation: 77157

In addition to the type issue pointed out in MK's fine answer, I noticed the csv example you pose has some whitespace issues that break your code. Here's 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)

Upvotes: 0

Anthon
Anthon

Reputation: 76792

In both cases you read through all of the values in the for statement, you should not. Just do:

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

Upvotes: 1

MK.
MK.

Reputation: 34587

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

You should probably do the inverse, i.e.

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

Upvotes: 7

Related Questions