Keoros
Keoros

Reputation: 1387

Python skip first line .readlines( )[1:] not working?

If I remove [1:], if works fine and prints all data.

f = open("test.csv", "r")
lines = f.readlines()
f.close()
print lines

result:

['title1,title2\raa,aaaa\rbb,bbbb\rcc,cccc']

but if I try to skip the first line by adding [1:]

f = open("test.csv", "r")
lines = f.readlines()[1:]
f.close()
print lines

it prints an empty array

[]

I'm using python 2.7.6. Does anyone know why?

Upvotes: 0

Views: 5461

Answers (1)

Mike Pennington
Mike Pennington

Reputation: 43077

result:

 ['title1,title2\raa,aaaa\rbb,bbbb\rcc,cccc']

but if I try to skip the first line by adding [1:] it prints empty array

It looks like you have a platform line encoding issue. You're assuming that python reads this as a multi-line file; however, python only sees one line.

Modify your code to do this...

f = open("test.csv", "r")
lines = f.read().splitlines()   # Thanks to Ashwini's comment for tip
f.close()
print lines

Upvotes: 1

Related Questions