10

I couldn´t find a better place to ask my question. I am learning Python and trying to create a script as follows.

1) Should be able to search csv file.

2) Return entire row if match is found.

My csv:

Product,Scan,Width,Height,Capacity
LR,2999,76,100,17.5
RT,2938,37,87,13.4

If I search for 2938 for an example, entire row is returned as follows:

Product: RT
Scan: 2938
Width: 37
Height: 87
Capacity: 13,4

So far I have:

csvFile = getComponent().filePath
pos = csvFile.rfind('Desktop\\')
csvFile = csvFile[:pos] + 'programm\\products.csv'

myfile = open(csvFile)
myfile.seek(0)
for line in myfile.split('\n'):
    data = line.split(',')
    print data
    if data[2] == agv_O_Cal.value and data[3] == agv_O_Mod.value:
        print 'found: value = %s' %agv_O_Cal.value, agv_O_Mod.value
        Product = data[5]
        Scan = data[6]
        Width = data[7]
        Height = data[9]
        Capacity = data[10]
        print , Product, Scan, Width, Height, Capacity

The solution doesn´t work.

Hasan Ramezani
  • 5,004
  • 24
  • 30
Meigo62
  • 163
  • 1
  • 2
  • 12

2 Answers2

10
#!/usr/bin/python

import csv
import sys

#input number you want to search
number = raw_input('Enter number to find\n')

#read csv, and split on "," the line
csv_file = csv.reader(open('test.csv', "r"), delimiter=",")


#loop through the csv list
for row in csv_file:
    #if current rows 2nd value is equal to input, print that row
    if number == row[1]:
         print (row)
riyansh.legend
  • 117
  • 1
  • 13
smushi
  • 701
  • 6
  • 17
  • Should my csv file be located in the same folder as py file? – Meigo62 Sep 28 '14 at 07:17
  • yes, ofcourse replace "test.csv" with your csv name – smushi Sep 28 '14 at 07:18
  • Enter number to find 2938 Traceback (most recent call last): File "C:\Users\MyName\Desktop\programm\phy2.py", line 5, in csv_file = csv.reader(open('autod.csv', "rb"), delimiter=",") NameError: name 'csv' is not defined – Meigo62 Sep 28 '14 at 07:23
  • How is possible to output as a list? Description before (ex. Product: Xcl where Xcl is value taken from csv) and value after. – Meigo62 Sep 28 '14 at 12:39
  • didnt understand your question, but please upvote/accept my answer as per community standards. – smushi Sep 28 '14 at 15:00
  • What if I want to search all columns? Is it possible to search realtime? Is it possible to return results as a list? – Meigo62 Sep 29 '14 at 05:40
  • yes it is, think about it, try things then if you need help post on here for help – smushi Sep 29 '14 at 06:08
  • I got this error @smushi File "fil.py", line 17 print row ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(row)? – Kevin Nguyen Mar 05 '20 at 12:09
  • `csv.reader(open('test.csv', "rb"), delimiter=",")` - change rb to r in this line. It gave me this error without it. `_csv.Error: iterator should return strings, not bytes` – hispeed Jun 05 '20 at 09:32
  • @Kevin Nguyen, in Python3, the print became a callable, so you have to type `print(row)` instead of `print row` which worked fine in Python2 – CVerica Mar 21 '22 at 21:38
1

you can use csv module like this:

import csv
reader = csv.reader(open(csvFile, 'r'))
for data in reader:
    #list index start from 0, thus 2938 is in data[1]
    if data[1] == agv_O_Cal.value and data[3] == agv_O_Mod.value:
        #do somethinngs
Hasan Ramezani
  • 5,004
  • 24
  • 30