What is the advantage of using csv.reader over writing my own parser in python

user2225190

If below two code snippets are giving same results then please let me know what is the advantage of using csv.reader

1)

import csv
f = open('a.csv', 'rb')
spamreader = csv.reader(f)
for a in spamreader:
    print a

2)

f = open('a.csv', 'rb')
for a in f:
    print a.split(',')

Result:

['SNO', ' Name', ' Dept']
['1', ' Def', ' Electronics']
['2', 'Abc', 'Computers']
Martin Thoma

In your example, I don't see an advantage of using the csv module. However, things change when you have quoted elements:

SNO,Name,Dept
1,Def,Electronics
2,Abc,Computers
3,"here is the delimiter, in quotes",ghi

With the csv module, it is simply

import csv
with open('a.csv', 'rb') as f:
    csv_reader = csv.reader(f, delimiter=',', quotechar='"')
    for row in csv_reader:
        print(row)

but splitting would ignore the quotes.

(Anyway, I recommend using pandas as shown here for reading CSV files. Please also note that you should close files you've opened. By using the with statement, you can do it implicity.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What is the added advantage of using Bazel over Gradle?

From Dev

Freemarker What is the advantage of using TemplateMethodModelEx over TemplateMethodModel

From Dev

What is the advantage of using annotation over interface type?

From Dev

What is the advantage of N-Triples over CSV for storing RDF triples?

From Dev

What is the advantage of N-Triples over CSV for storing RDF triples?

From Dev

Using my own .csv in tensorflow

From Dev

What is the advantage of using bash -c over using a here string?

From Dev

What is the advantage of using Path.Combine over concatenating strings with '+'?

From Dev

What's the advantage of using enum string over string in D?

From Dev

What is the advantage of using a git tag over an empty commit in git?

From Dev

What is the advantage of using a date dimension table over directly storing a date?

From Dev

What is the advantage of using Hash code over HTTP Connection

From Dev

What is the advantage of using an Dynamic object over creating a class of that object?

From Dev

what is the advantage of using Alamofire over NSURLSession/NSURLConnection for networking?

From Dev

CakePHP - What is the advantage of using CakePHP functions over 'normal' PHP functions?

From Dev

What is the advantage of using a date dimension table over directly storing a date?

From Dev

what is the advantage of using Alamofire over NSURLSession/NSURLConnection for networking?

From Dev

What is the advantage of using SSL/TLS over just handling encryption myself

From Dev

What is exact ModeShape advantages over writing own mongodb storage?

From Dev

Ruby: CSV parser tripping over double quotation in my data

From Dev

What is the advantage of iSCSI over SMB?

From Dev

Writing my own 'TreeMap' class - what to extend and/or implement?

From Dev

What is the advantage of using CocoaPods?

From Dev

What is the advantage of using ObjectSet

From Dev

What is the advantage of using arrayWithCapacity

From Dev

Is there an advantage to using types over records?

From Dev

Advantage of EventListenerList over using Vector

From Dev

Is there an advantage to using .data over a for loop?

From Dev

Advantage of using new Integer(a) over a

Related Related

  1. 1

    What is the added advantage of using Bazel over Gradle?

  2. 2

    Freemarker What is the advantage of using TemplateMethodModelEx over TemplateMethodModel

  3. 3

    What is the advantage of using annotation over interface type?

  4. 4

    What is the advantage of N-Triples over CSV for storing RDF triples?

  5. 5

    What is the advantage of N-Triples over CSV for storing RDF triples?

  6. 6

    Using my own .csv in tensorflow

  7. 7

    What is the advantage of using bash -c over using a here string?

  8. 8

    What is the advantage of using Path.Combine over concatenating strings with '+'?

  9. 9

    What's the advantage of using enum string over string in D?

  10. 10

    What is the advantage of using a git tag over an empty commit in git?

  11. 11

    What is the advantage of using a date dimension table over directly storing a date?

  12. 12

    What is the advantage of using Hash code over HTTP Connection

  13. 13

    What is the advantage of using an Dynamic object over creating a class of that object?

  14. 14

    what is the advantage of using Alamofire over NSURLSession/NSURLConnection for networking?

  15. 15

    CakePHP - What is the advantage of using CakePHP functions over 'normal' PHP functions?

  16. 16

    What is the advantage of using a date dimension table over directly storing a date?

  17. 17

    what is the advantage of using Alamofire over NSURLSession/NSURLConnection for networking?

  18. 18

    What is the advantage of using SSL/TLS over just handling encryption myself

  19. 19

    What is exact ModeShape advantages over writing own mongodb storage?

  20. 20

    Ruby: CSV parser tripping over double quotation in my data

  21. 21

    What is the advantage of iSCSI over SMB?

  22. 22

    Writing my own 'TreeMap' class - what to extend and/or implement?

  23. 23

    What is the advantage of using CocoaPods?

  24. 24

    What is the advantage of using ObjectSet

  25. 25

    What is the advantage of using arrayWithCapacity

  26. 26

    Is there an advantage to using types over records?

  27. 27

    Advantage of EventListenerList over using Vector

  28. 28

    Is there an advantage to using .data over a for loop?

  29. 29

    Advantage of using new Integer(a) over a

HotTag

Archive