@@ -418,101 +418,50 @@ Examples
418
418
419
419
The simplest example of reading a CSV file::
420
420
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
- =======
428
421
import csv
429
422
with open('some.csv', newline='') as f:
430
423
reader = csv.reader(f)
431
424
for row in reader:
432
425
print(row)
433
- >>>>>>> other
434
426
435
427
Reading a file with an alternate format::
436
428
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
- =======
444
429
import csv
445
430
with open('passwd') as f:
446
431
reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
447
432
for row in reader:
448
433
print(row)
449
- >>>>>>> other
450
434
451
435
The corresponding simplest possible writing example is::
452
436
453
- <<<<<<< local
454
- import csv
455
- with f = open("some.csv", "w"):
456
- writer = csv.writer(f)
457
- writer.writerows(someiterable)
458
- =======
459
437
import csv
460
438
with open('some.csv', 'w') as f:
461
439
writer = csv.writer(f)
462
440
writer.writerows(someiterable)
463
- >>>>>>> other
464
441
465
442
10000
Since :func: `open ` is used to open a CSV file for reading, the file
466
443
will by default be decoded into unicode using the system default
467
444
encoding (see :func: `locale.getpreferredencoding `). To decode a file
468
445
using a different encoding, use the ``encoding `` argument of open::
469
446
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
- =======
477
447
import csv
478
448
with open('some.csv', newline='', encoding='utf-8') as f:
479
449
reader = csv.reader(f)
480
450
for row in reader:
481
451
print(row)
482
- >>>>>>> other
483
452
484
453
The same applies to writing in something other than the system default
485
454
encoding: specify the encoding argument when opening the output file.
486
455
487
456
Registering a new dialect::
488
457
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
- =======
497
458
import csv
498
459
csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE)
499
460
with open('passwd') as f:
500
461
reader = csv.reader(f, 'unixpwd')
501
- >>>>>>> other
502
462
503
463
A slightly more advanced use of the reader --- catching and reporting errors::
504
464
CB0B
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
- =======
516
465
import csv, sys
517
466
filename = 'some.csv'
518
467
with open(filename, newline='') as f:
@@ -522,7 +471,6 @@ A slightly more advanced use of the reader --- catching and reporting errors::
522
471
print(row)
523
472
except csv.Error as e:
524
473
sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e))
525
- >>>>>>> other
526
474
527
475
And while the module doesn't directly support parsing strings, it can easily be
528
476
done::
0 commit comments