how to read one complete row in the CSV

Asked by manisha

Hello,
I'm trying to read a complete row which is containing two three points in my CSV document. but it is reading first line of the row and come back to the script. Please help me to come out from the issue.

My script is:
import csv

# Enter file name details
file = input("Enter File name")
if (file == ""):
    popup("No file name given")
    exit(0)
# Enter Row number details
Row = int(input("Enter a row number"))
if(Row == ""):
    popup("no row number given")
    exit(0)
# main Function call
def lineFromCSV(file, Row):
    data = csv.reader(open(file), delimiter=' ', quotechar='|')
    for i in range(Row+1) :
        Row = data.next()
    return Row

def extractColInfo(temp):
    for i in range(7):
        tt[i] = temp[i]
        popup (str(tt[i]))

temp = lineFromCSV(file, Row)
#extractColInfo(temp)

popup( ', '.join(temp))
popup ("Done")

My csv is:

Sl No DO-229D STP ID Scenario Simulator Dump
1 2.1.1.2 (b)
        2.1.1.3.1 (b) STP-GPSBL-007 gpsb028 Spirent Iono_corrections_from_gps
2 2.1.1.2 (c )
        2.1.1.3.1 (c ) STP-GPSBL-008 gpsb028 Spirent Iono_corrections_from_waas[sv].validity
                                                                                                        No_of_svs_for_iono_corrections
3 2.1.1.2 (d)
        2.1.1.3.1 (d)
        2.1.1.2 (g) STP-GPSBL-009 gpsb028 Spirent Tropospheric_corrections [var_sv]

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

having unique, speaking names for different variable is vital.
Not obeying this rule leads to problems hard to debug.

In this case the same name Row is used for the row number and the row content.

   for i in range(Row+1) :
        Row = data.next()

after the first loop, Row contains the first row's content and simply terminates the loop.

So sort out your variables.

other problems in your script:

Row = int(input("Enter a row number"))
# this crashes, if no number is entered - usually wrapped with try:except:

if(Row == ""): # can never be, since Row now contains a number
    popup("no row number given")
    exit(0)

... and the next

def extractColInfo(temp):
    for i in range(7):
        tt[i] = temp[i]
        popup (str(tt[i]))

tt is not defined an hence cannot be used with a list index

def extractColInfo(temp):
    tt = [] # empty list
    for i in range(7):
        tt.append(temp[i]) # add to the end of the list
        popup (str(tt[i])) # now the index i is valid

Revision history for this message
manisha (manishareddy1111) said :
#2

Thanks RaiMan, that solved my question.