0

I have a csv file with a 1st part made up of 3 entries and 2nd part made up of 2 entries. I want to isolate the data made up of 3 entries.

i wrote the following:

filename=open("datafile.txt","r")
data_3_entries_list=[]

 for line in filename:
    fields=line.split(",")
    if fields[2] == 0:
        break
    else:
        data_3_entries_list.extend(line)

i get the error message:
if fields[2] == 0 : IndexError: list index out of range print(data_3_entries_list)

I also tried with if fields[2] is None but i get the same error. I dont understand why im getting this error?

11
  • 2
    you should probably test the length of fields first e.g. if len(fields) >= 3 Commented Sep 18, 2014 at 12:34
  • can you provide the csv file please? Commented Sep 18, 2014 at 12:35
  • I originally stated a length of 2 it should be 3 as indices are 0 based Commented Sep 18, 2014 at 12:36
  • I would agree, test the length of the list. Also, you should probably close the file when you're done. Commented Sep 18, 2014 at 12:38
  • @Edchum I changed it to what you said and now nothing happens when I run it Commented Sep 18, 2014 at 12:43

4 Answers 4

2

Use len() or str.count()

for line in filename:
    fields=line.split(",")
    if len(fields) == 3:
        data_3_entries_list.append(line)

for line in filename:
    if fields.count(",") == 2:
        data_3_entries_list.append(line)
Sign up to request clarification or add additional context in comments.

2 Comments

although at the end of each line in the list i get /n what does it mean? @xxyzzy
I suppose that /n represents end-of-line, whereas Python uses \n. You would have to use string.replace().
1

There is no implicit value for non-existent elements of a list; if fields only has 2 items, then fields[2] simply does not exist and will produce an error.

Check the length of the list explicitly:

if len(fields) == 3:
    break
data_3_entries_list.append(line)  # You may want append here, not extend

Comments

0

If i have a csv file like this one, then this can work out

 with open('datafile.csv', 'w') as p:
    p.write("Welcome,to,Python")
    p.write("Interpreted,Programming,Language")

 filename=open("datafile.csv","r")
 data_3_entries_list=[]

 for line in filename:
    fields=line.split(",")
    if fields[2] == 0:
        break
    else:
        data_3_entries_list.extend(line)

Otherwise you'd have to atleast show us or me how your csv file is formatted.

Comments

0

You're getting the error because you're trying to get the value of an index that may not exist. There's a better way to do this, though.

Instead of testing the length outright, you can also use a split to see if the index exists. A list that isn't len(a_list) >= 3 will simply return an empty list.

>>> mylist = range(3) #this list is [0, 1, 2]
>>> mylist[2:]
[2]

>>> mylist = range(2) #however THIS list is [0, 1]
>>> mylist[2:]
[]

In this way you can use the pythonic if a_list: do_work() to test if you even want to work with the data. This syntax is generally preferred to if len(a_list) == value: do_work().

filename=open("datafile.txt","r")
data_3_entries_list=[]

 for line in filename:
    fields=line.split(",")
    if fields[2:]:  ##use a split to verify that such a split yields a result
        data_3_entries_list.extend(line)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.