Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: how do i always start from the second row in csv?

Tags:

python

csv

b holds the contents of a csv file

i need to go through every row of b; however, since it has a header, i dont want to pay attention to the header. how do i start from the second row?

for row in b (starting from the second row!!):
like image 525
Alex Gordon Avatar asked Aug 02 '10 22:08

Alex Gordon


People also ask

How do I start the second line of a CSV file in Python?

Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open() method. Step 2: Create a reader object by passing the above-created file object to the reader function. Step 3: Use for loop on reader object to get each row.

How do you add a row at the beginning of a CSV file Python?

Open your CSV file in append mode Create a file object for this file. Pass the file object and a list of column names to DictWriter() You will get an object of DictWriter. Pass the dictionary as an argument to the writerow() function of DictWriter (it will add a new row to the CSV file).

How do I iterate over a row in a CSV file?

In order to iterate over rows, we can use three function iteritems(), iterrows(), itertuples() . These three function will help in iteration over rows.

Can you index CSV reader?

Reading and Writing CSV Files Reader() and the second uses csv. DictReader() . csv. Reader() allows you to access CSV data using indexes and is ideal for simple CSV files.


2 Answers

Prepend a next(b) (in every recent version of Python; b.next() in older ones) to skip the first row (if b is an iterator; if it is, instead, a list, for row in b[1:]:, of course).

like image 134
Alex Martelli Avatar answered Sep 30 '22 15:09

Alex Martelli


b.next()
for row in b:
    # do something with row

But consider using the csv module, especially with DictReader.

like image 33
Matthew Flaschen Avatar answered Sep 30 '22 16:09

Matthew Flaschen