Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: searching csv and return entire row

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.

like image 448
Meigo62 Avatar asked Sep 28 '14 06:09

Meigo62


People also ask

How to read rows from a CSV file in Python?

Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open () method. While loading the file by specifying path along with filename, if you got any unicode error then append r before path of filename

How to parse a CSV file in Python?

Parsing CSV Files With Python’s Built-in CSV Library 1 Reading CSV Files With csv. Reading from a CSV file is done using the reader object. ... 2 Reading CSV Files Into a Dictionary With csv. ... 3 Optional Python CSV reader Parameters. ... 4 Writing CSV Files With csv. ... 5 Writing CSV File From a Dictionary With csv. ...

How to retrieve entire row or column of an array in Python?

How to Retrieve an Entire Row or Column of an Array in Python? 1 Python. import numpy as np. arr1 = np.array ( ["Ram", "Shyam" , "Sita"]) print("First row - ") print(arr1) print ("First Column") print (arr1 [0]) ... 2 Python3. 3 Python3. 4 Python3.

What does the first row of a CSV file contain?

The first row returned contains the column names, which is handled in a special way. Rather than deal with a list of individual String elements, you can read CSV data directly into a dictionary (technically, an Ordered Dictionary) as well.


2 Answers

#!/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)
like image 122
smushi Avatar answered Oct 14 '22 16:10

smushi


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
like image 41
Hasan Ramezani Avatar answered Oct 14 '22 17:10

Hasan Ramezani