diff options
author | Skip Montanaro <skip@pobox.com> | 2011-03-19 18:05:58 (GMT) |
---|---|---|
committer | Skip Montanaro <skip@pobox.com> | 2011-03-19 18:05:58 (GMT) |
commit | b7d1aebb6697855dbb0d9d3333d9af2b90d78306 (patch) | |
tree | a51f35c9b75217b4b12b2dd7c679f44aef62894f /Doc/library/csv.rst | |
parent | 482e7e061b110d4c2a5b8d39d13444370baf112b (diff) | |
download | cpython-b7d1aebb6697855dbb0d9d3333d9af2b90d78306.zip cpython-b7d1aebb6697855dbb0d9d3333d9af2b90d78306.tar.gz cpython-b7d1aebb6697855dbb0d9d3333d9af2b90d78306.tar.bz2 |
*sigh* - i don't like this workflow at all
Diffstat (limited to 'Doc/library/csv.rst')
-rw-r--r-- | Doc/library/csv.rst | 52 |
1 files changed, 0 insertions, 52 deletions
diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 892efb1..b166d85 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -418,101 +418,50 @@ Examples The simplest example of reading a CSV file:: -<<<<<<< local - import csv - with f = open("some.csv", newline=''): - reader = csv.reader(f) - for row in reader: - print(row) -======= import csv with open('some.csv', newline='') as f: reader = csv.reader(f) for row in reader: print(row) ->>>>>>> other Reading a file with an alternate format:: -<<<<<<< local - import csv - with f = open("passwd"): - reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE) - for row in reader: - print(row) -======= import csv with open('passwd') as f: reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE) for row in reader: print(row) ->>>>>>> other The corresponding simplest possible writing example is:: -<<<<<<< local - import csv - with f = open("some.csv", "w"): - writer = csv.writer(f) - writer.writerows(someiterable) -======= import csv with open('some.csv', 'w') as f: writer = csv.writer(f) writer.writerows(someiterable) ->>>>>>> other Since :func:`open` is used to open a CSV file for reading, the file will by default be decoded into unicode using the system default encoding (see :func:`locale.getpreferredencoding`). To decode a file using a different encoding, use the ``encoding`` argument of open:: -<<<<<<< local - import csv - f = open("some.csv", newline='', encoding='utf-8'): - reader = csv.reader(f) - for row in reader: - print(row) -======= import csv with open('some.csv', newline='', encoding='utf-8') as f: reader = csv.reader(f) for row in reader: print(row) ->>>>>>> other The same applies to writing in something other than the system default encoding: specify the encoding argument when opening the output file. Registering a new dialect:: -<<<<<<< local - import csv - csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE) - with f = open("passwd"): - reader = csv.reader(f, 'unixpwd') - for row in reader: - pass -======= import csv csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE) with open('passwd') as f: reader = csv.reader(f, 'unixpwd') ->>>>>>> other A slightly more advanced use of the reader --- catching and reporting errors:: -<<<<<<< local - import csv, sys - filename = "some.csv" - with f = open(filename, newline=''): - reader = csv.reader(f) - try: - for row in reader: - print(row) - except csv.Error as e: - sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e)) -======= import csv, sys filename = 'some.csv' with open(filename, newline='') as f: @@ -522,7 +471,6 @@ A slightly more advanced use of the reader --- catching and reporting errors:: print(row) except csv.Error as e: sys.exit('file {}, line {}: {}'.format(filename, reader.line_num, e)) ->>>>>>> other And while the module doesn't directly support parsing strings, it can easily be done:: |