0

I have been reading up on the csv.reader next but did not see a way to compare the values in a column from one row to the next. For instance, if my data looked like this in Maps.csv file:

County1     C:/maps/map1.pdf 
County1     C:/maps/map2.pdf 
County2     C:/maps/map1.pdf 
County2     C:/maps/map3.pdf 
County3     C:/maps/map3.pdf 
County4     C:/maps/map2.pdf 
County4     C:/maps/map4.pdf

If line two's county equals line one's county do something

The following code compares rows, I want to compare the county values between current and previous rows.

import csv.

f = open("Maps.csv", "r+")
ff = csv.reader(f)

pre_line = ff.next()
while(True):
    try:
        cur_line = ff.next()
        if pre_line == cur_line:
            print "Matches"
        pre_line = cur_line
    except:
        break

I know I can grab the current value (see below) but do not know how to grab previous value. Is this possible? If so, could someone please tell me how. On day three of trying to solve writing my script to append pdf files from a csv file and am about to toss my coffee cup at my monitor. I am breaking these down into smaller parts and using simpler data as pilot. My file is much larger. I was advised to focus on just one issue at a time when posting to this forum. This is my latest issue. It seems no matter what tack I take, I can't seem to read the data the way I want. Arrrggghhhhh.

CurColor = row[color]

Using python 2.7

1
  • just read your csv file into a list of rows: rows = list(ff). Now you have the whole csv in memory as lists of lsts Commented Sep 14, 2017 at 20:17

2 Answers 2

1

You already know how to look up the previous row. Why not get the column you need from that row?

import csv.

f = open("Maps.csv", "r+")
ff = csv.reader(f)

pre_line = ff.next()
while(True):
    try:
        cur_line = ff.next()
        if pre_line[0] == cur_line[0]: # <-- compare first column
            print "Matches"
        pre_line = cur_line
    except:
        break

or more simply:

pre_line = ff.next()
for cur_line in ff:
    if pre_line[0] == cur_line[0]: # <-- compare first column
        print "Matches"
    pre_line = cur_line
Sign up to request clarification or add additional context in comments.

Comments

0
import csv

f = open("Maps.csv", "r+")
# Use delimiters to split each line into different elements
# In my example i used a comma. Your csv may have a different delimiter
# make sure the delimiter is a single character string though
# so no multiple spaces between "County1     C:/maps/map1.pdf"
# it should be something like "County1,C:/maps/map1.pdf"
ff = csv.reader(f, delimiter=',')

COUNTY_INDEX = 0

# each time ff.next() is called, it makes an array variable ['County1', 'C:/maps/map1.pdf ']
# since you want to compare the value in the first index, then you need to reference it like so
# the line below will set pre_line = 'County1'
pre_line = ff.next()[COUNTY_INDEX]
while(True):
    try:
        # the current line will be 'County1' or 'County2' etc...Depending on which line is read
        cur_line = ff.next()[COUNTY_INDEX]
        if pre_line == cur_line:
            print "Matches"
        pre_line = cur_line
    except:
        break

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.