Slicing lists imported from CSV files

I have a list of 156 elements (all integers) in a CSV file. I'm importing the list using the following code:

    with open("rayuela-chapter-list.txt") as f:
        r = csv.reader(f)
        x = [row for row in r]

When I try to slice the list I get the errors like the one below:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    IndexError: list index out of range

When it does not give me an error it returns the entire list, no matter how I slice it.

If I'm importing a CSV file, how do I make sure that I can slice the resulting list? is there anything I'm missing in the code?


If the list is stored in csv as a row, you are reading this whole row in one take. Basically, this line

x = [row for row in r]

will return


If there would be more rows, it would be a list of lists, but in your case, it is a list of one list. To get what you need, just do:

x = x[0]

Need Your Help

Some approaches to distribute an iPhone application to run in a specific location

iphone app-store distribution software-distribution

My boss wants to distribute the application for a specific client, whose number of employees is around 500. The application uses webservices and the UDID of the device to restrict the other users f...

Action listener for a JTextField to change value in another textfield

java swing actionlistener jtextfield

I have two JTextfields, I want the value/text of second Textfield to change based on any change in value/text made in first TextField