@@ -418,101 +418,50 @@ Examples
418418
419419The simplest example of reading a CSV file::
420420
421- <<<<<<< local
422- import csv
423- with f = open("some.csv", newline=''):
424- reader = csv.reader(f)
425- for row in reader:
426- print(row)
427- =======
428421 import csv
429422 with open('some.csv', newline='') as f:
430423 reader = csv.reader(f)
431424 for row in reader:
432425 print(row)
433- >>>>>>> other
434426
435427Reading a file with an alternate format::
436428
437- <<<<<<< local
438- import csv
439- with f = open("passwd"):
440- reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
441- for row in reader:
442- print(row)
443- =======
444429 import csv
445430 with open('passwd') as f:
446431 reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
447432 for row in reader:
448433 print(row)
449- >>>>>>> other
450434
451435The corresponding simplest possible writing example is::
452436
453- <<<<<<< local
454- import csv
455- with f = open("some.csv", "w"):
456- writer = csv.writer(f)
457- writer.writerows(someiterable)
458- =======
459437 import csv
460438 with open('some.csv', 'w') as f:
461439 writer = csv.writer(f)
462440 writer.writerows(someiterable)
463- >>>>>>> other
464441
465442Since :func: `open ` is used to open a CSV file for reading, the file
466443will by default be decoded into unicode using the system default
467444encoding (see :func: `locale.getpreferredencoding `). To decode a file
468445using a different encoding, use the ``encoding `` argument of open::
469446
470- <<<<<<< local
471- import csv
472- f = open("some.csv", newline='', encoding='utf-8'):
473- reader = csv.reader(f)
474- for row in reader:
475- print(row)
476- =======
477447 import csv
478448 with open('some.csv', newline='', encoding='utf-8') as f:
479449 reader = csv.reader(f)
480450 for row in reader:
481451 print(row)
482- >>>>>>> other
483452
484453The same applies to writing in something other than the system default
485454encoding: specify the encoding argument when opening the output file.
486455
487456Registering a new dialect::
488457
489- <<<<<<< local
490- import csv
491- csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
492- with f = open("passwd"):
493- reader = csv.reader(f, 'unixpwd')
494- for row in reader:
495- pass
496- =======
497458 import csv
498459 csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
499460 with open('passwd') as f:
500461 reader = csv.reader(f, 'unixpwd')
501- >>>>>>> other
502462
503463A slightly more advanced use of the reader --- catching and reporting errors::
504464
505- <<<<<<< local
506- import csv, sys
507- filename = "some.csv"
508- with f = open(filename, newline=''):
509- reader = csv.reader(f)
510- try:
511- for row in reader:
512- print(row)
513- except csv.Error as e:
514- sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
515- =======
516465 import csv, sys
517466 filename = 'some.csv'
518467 with open(filename, newline='') as f:
@@ -522,7 +471,6 @@ A slightly more advanced use of the reader --- catching and reporting errors::
522471 print(row)
523472 except csv.Error as e:
524473 sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
525- >>>>>>> other
526474
527475And while the module doesn't directly support parsing strings, it can easily be
528476done::
0 commit comments