ben olsen
ben olsen

Reputation: 703

Python CSV specific line to extract

I have two CSV files and I want python to open file1.csv and read line 7 from that file and look for that same binary code on the WHOLE file2.csv.

This is what I have so far but it does not work:

import csv

a = open('file1.csv','r').readline[7]


with open('file2.csv') as infile:
    for row in csv.reader(infile):
        if row[1:] == a: # This part is fine because i want to skip the first row 
            print row[0], ','.join(row[1:])

Upvotes: 3

Views: 4553

Answers (1)

Taylan Aydinli
Taylan Aydinli

Reputation: 4363

Looks like you need to read up on how the python csv library works :) You might also want to read up on how list slicing works. I'll try to help you based on what I understood about your problem.

I have the same question that @oliver-w had but I'll just assume your 'csv' files have only one column.

import csv

with open('file1.csv', 'r') as file1:
    # this is the value you will be searching for in file2.csv
    # you might need to change this to [6] if there is no header row in file1.csv
    val = list(csv.reader(file1))[7]

with open('file2.csv', 'r') as file2:
    reader = csv.reader(file2)
    reader.next() # this skips the first row of the file
    # this iteration will start from the second row of file2.csv
    for row in reader:
        if row[0] == val:
            # your question doesn't clarify what your actual purpose is
            # so i don't know what should be here

Upvotes: 2

Related Questions