top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to read CSV file in python?

+2 votes
1,095 views

Is there a better way to do that:

def Read_CSV_File(filename):
 file = open(filename, "r")
 reader = csv.DictReader(file)
 line = 1
 for row in reader:
 if line < 6:
 reader.next()
 line++
# process the CSV
posted Dec 17, 2013 by Jai Prakash

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
This one is giving me a compilation error.

    [root@satya user]# python sample.py
      File "sample.py", line 9
        line++
             ^
    SyntaxError: invalid syntax

3 Answers

+1 vote

You can use the following way to do this using "with" operator.

def Read_CSV_File(filename):       with open(filename, "r") as csvfile:
          csvreader = csv.DictReader(csvfile)
           line = 1
          for row in csvreader:
               if line < 6:
                 reader.next()
                  line++                 continue
     # process the CSV

Rest of the things are pretty much straightforward.

answer Dec 17, 2013 by anonymous
+1 vote
line++
is a syntax error in Python. If you fix that

line = 1
for row in reader:
 if line < 6:
 reader next()
 line += 1

You are still reading the complete csv file. Assuming
(1) the first row of the csv contains the column names
(2) you want to skip the first five rows of data

you'd have to write

reader = csv.Reader(file)
line = 0
while line < 5:
 next(reader)
 line += 1
for row in reader:
 .... # process csv row

A simpler alternative is to use itertools.islice():

for row in itertools.islice(reader, 5, None):
 ... # process csv row
answer Dec 17, 2013 by anonymous
+1 vote

If you want to read line 6th row use this :

file = open("satya.csv","r")
reader = csv.reader(file)
line = 1
for row in reader:
        if line == 6:
                print row
        line = line+1
answer Dec 17, 2013 by Satyabrata Mahapatra
Similar Questions
+1 vote

I've a huge csv file and I want to read stuff from it again and again. Is it useful to pickle it and keep and then unpickle it whenever I need to use that data? Is it faster that accessing that file simply by opening it again and again? Please explain?

+1 vote

I just wanted to see the best way to securely accomplish this task. When we want to update a DB we upload to a writable directory instead of writing it directly to MySQL, I don't like having writable directories if possible.
Is there a right or better way to accomplish this?

0 votes

The problem is that some of the fields contain commas, but they are inside double quotes.

Example:

sort -t, -k1,1 -k3,3 -k2,2 SomeFile.csv > OutputFile.csv

A line could look something like this:
This is the first field,"This is, well, the second field",The third field could look like this

That line has three fields:
1: This is the first field
2: "This is, well, the second field"
3: The third field could look like this

But sort consider it to have five fields:
1: This is the first field
2: "This is
3: well
4: the second field
5: The third field could look like this

How would you solve this?

...