0

I am new to python programming. I need to read contents from a csv file and print based on a matching criteria. The file contains columns like this:

abc, A, xyz, W
gfk, B, abc, Y, xyz, F

I want to print the contents of the adjacent column based on the matching input string. For e.g. if the string is abc it should print A, and W for xyz, and "no match" for gfk for the first row. This should be executed for each row until the end of the file.

I have the following code. However, don't know to select the adjacent column.

c= ['abc','xyz','gfk']
with open('filer.csv', 'rt') as csvfile:
    my_file = csv.reader(csvfile, delimiter=',')
    for row in my_file:
        for i in c: 
            if i in row:
                print the contents of the adjacent cell 

I would appreciate any help in completing this script.

Thank you

Richard
  • 721
  • 5
  • 16
M. Nair
  • 5
  • 5

2 Answers2

1

Your approach made it more difficult to print adjacent values, because even if you used enumerate to get the indices, you would have to search the row again, after finding each pattern (after if i in row: you wouldn't immediately know where it was in the row). By structuring the data in a dictionary it becomes simpler:

patterns = ['abc','xyz','gfk']
with open('filer.csv') as fh:
    reader = csv.reader(fh, skipinitialspace=True)
    for line in reader:
        print '---'
        content = dict(zip(line[::2], line[1::2]))
        for pattern in patterns:
            print "{}: {}".format(pattern, content.get(pattern, 'no match'))

zip(line[::2], line[1::2]) creates a list of tuples from the adjacent elements of the list, which can be turned into a dictionary, where the patterns you are looking for are keys and the corresponding letters are values.

Paulo Almeida
  • 7,803
  • 28
  • 36
  • Thank you for your reply, the output I get is as follows. As you can see only the last tuple is matched. --- abc: A xyz: no match gfk: no match --- abc: no match xyz: no match gfk: B – M. Nair Sep 05 '15 at 21:36
  • You have spaces in your file, you need to strip them if they are not meaningful. – Paulo Almeida Sep 05 '15 at 21:56
  • Replace the third line with `reader = csv.reader(fh, skipinitialspace=True)`. – Paulo Almeida Sep 05 '15 at 22:00
  • Is it possible to read the first element as a text and the second element of the dictionary as an integer from a csv file. For e.g.as 'A': 4, 'B':3, 'F':0 rather than 'A':'4', 'B':'3', 'F':'0'. Thanks – M. Nair Sep 06 '15 at 12:55
  • Yes, you can replace `line[1::2]` with `map(int, line[1::2])`. – Paulo Almeida Sep 06 '15 at 15:32
0

You can adapt this for the CSV, but it basically do what you ask for

csv = [['abc', 'A', 'xyz', 'W'],
       ['gfk', 'B', 'abc', 'Y', 'xyz', 'F']]

match_string = ['abc','xyz','gfk']

for row in csv:
    for i, column_content in enumerate(row):
        if column_content in match_string:
            print row[i + 1]  # + 1 for the following column_content 'A', 'W', etc.

You should read about slicing : Explain Python's slice notation

Community
  • 1
  • 1
Richard
  • 721
  • 5
  • 16