English 中文(简体)
使用 python 搜索 csv 的十六进制数据
原标题:Searching a csv for hex data with python
  • 时间:2012-05-25 20:27:53
  •  标签:
  • python
  • csv

我有一个 Csv 文件, 它包含 3 列 。 我正试图通过 第二 列查找 特定值 (hex 值), 并读取该行的下一个条目 (第 3 栏) 。 格式与下面相似 :

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

我搜索和读取最后的值(0x04413053)和打印值(8)没有问题。然而,当我试图搜索前7个条目中的任何1个条目时,没有任何东西被读回(在输出上 ) 。 我的代码在下面,有人知道错误是什么吗?

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
最佳回答

xx (0x 0 4413053) 是"0x4413053"

你也许应该做反面的,即:

int(line[clolumn1], 16) == 0x04413053
问题回答

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

除了在

问题在于条件。 您没有比较相同的值 。

  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

在玩弄所有例子之后,我达成了以下工作守则:

Anthon 是正确的, 我只需要在列表中排序一次这个值, 这样就不存在重复模式 。 我确实要修改 Anthon 来添加 MK 的输入, 以找到我正在寻找的十六进制值 。

谢谢你的帮助

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




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...