1
votes

I am trying to store the 2nd 1 delimited spot in a csv UTF-8 file. My logic seems right but I KEEP GETTING an index out of range error which I can't seem to extrapolate. Here is my code: mylist_n = [] with open(r'/home/main/Documents/AMCO/mypythonAMCO/ralco_Info.csv') as csvfile: spamreader = csv.reader(csvfile,delimiter=',', quotechar='|') reader = csv.reader(csvfile)

    for row in spamreader:

        print(', '.join(row))
        mylist_n.append(row[1])

'''


IndexError                                Traceback (most recent call last)
<ipython-input-46-5b4beea38798> in <module>
      6 
      7         print(', '.join(row))
----> 8         print(row[3]) #read the make -- write to list

IndexError: list index out of range

I know it has something to do with the output of row and a particular module/function call. That's all I need. I plan on getting position 3, storing it in a list and other respective positions related to make/model and eventually making a dictionary of dates with respective values to make the otherwise extraordinarily long name condensed.

sam

1
Index numbering starts with 0, therefore 3 means the 4th item. - Michael Butscher
sample input file will help ... - sandeep rawat
SO I just found out the csv has actually only 2 columns. Now I guess the questions is what IS row? What type of function is, what is its input and outputs and what respective functions can I use to turn row[1] into a string of data to be stored in a list to be used in conjunction with the csvwriter. - Cameron Carter

1 Answers

0
votes
with open(r'/home/main/Documents/AMCO/mypythonAMCO/ralco_Info.csv') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    #reader = csv.reader(csvfile)

    for row in spamreader:
        #print("->".join(csvfile))

        #print(row[1])

    make_n.append(row[1].lower)
    print("appended")
    print(row)
    print("\n")

To make a long story short, make sure to watch out for your comma's in CSV and I am to bolster my string manipulation and json skills tonight so I can parse through these individual strings within the list of which contain comma's from the source data.