简体   繁体   中英

Taking a specific range of data in a CSV file (Python)

Basically, what I want to do here is read in a specific range of data (Say, 10,000 values) and see if it contains a match that I'm looking for. If it doesn't contain that match, then it throws out those values and takes the next 10,000.

For example, if I have the MD5 hash "fac2a47adace059aff113283a03f6760" (The value of which is stack), I will load 10,000 values from a CSV file and check to see if the MD5 hash in that line matches up with my given hash. If it does, then I print out the value after the comma on that line, and if it doesn't then throw those 10,000 values out of memory and take the 10,000 after that until I get a value.

Apologies of this is a bit unclear... I can't think of a crystal-clear way of explaining it. My current method of doing things is dumping a dictionary containing all the combinations of characters (up to 5) to a text file via JSON and loading that back into memory to be searched, which doesn't work with 5 characters (Throws a MemoryError).

Thanks in advance for any help, and let me know if you need clarification!

Assuming that the matching line looks like 'fac2a47adace059aff113283a03f6760,stack', you basically want to do this:

for row in csv.reader(csvfile):
    if row[0] == "fac2a47adace059aff113283a03f6760":
        print row[1]
        break

If your hash isn't in the first column or your pre-hash value isn't in the second, adjust the [0] and [1] to the right indexes.

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