Debian Codegraph @

petl/io/csv.py

raw
1 # -*- coding: utf-8 -*-
2 from __future__ import absolute_import, print_function, division
3
4
5 # standard library dependencies
6 from petl.compat import PY2
7
8
9 # internal dependencies
10 from petl.util.base import Table
11 from petl.io.sources import read_source_from_arg, write_source_from_arg
12 if PY2:
13 from petl.io.csv_py2 import fromcsv_impl, tocsv_impl, appendcsv_impl, \
14 teecsv_impl
15 else:
16 from petl.io.csv_py3 import fromcsv_impl, tocsv_impl, appendcsv_impl, \
17 teecsv_impl
18
19
20 def fromcsv(source=None, encoding=None, errors='strict', header=None,
21 **csvargs):
22 """
23 Extract a table from a delimited file. E.g.::
24
25 >>> import petl as etl
26 >>> import csv
27 >>> # set up a CSV file to demonstrate with
28 ... table1 = [['foo', 'bar'],
29 ... ['a', 1],
30 ... ['b', 2],
31 ... ['c', 2]]
32 >>> with open('example.csv', 'w') as f:
33 ... writer = csv.writer(f)
34 ... writer.writerows(table1)
35 ...
36 >>> # now demonstrate the use of fromcsv()
37 ... table2 = etl.fromcsv('example.csv')
38 >>> table2
39 +-----+-----+
40 | foo | bar |
41 +=====+=====+
42 | 'a' | '1' |
43 +-----+-----+
44 | 'b' | '2' |
45 +-----+-----+
46 | 'c' | '2' |
47 +-----+-----+
48
49 The `source` argument is the path of the delimited file, all other keyword
50 arguments are passed to :func:`csv.reader`. So, e.g., to override the
51 delimiter from the default CSV dialect, provide the `delimiter` keyword
52 argument.
53
54 Note that all data values are strings, and any intended numeric values will
55 need to be converted, see also :func:`petl.transform.conversions.convert`.
56
57 """
58
59 source = read_source_from_arg(source)
60 csvargs.setdefault('dialect', 'excel')
61 return fromcsv_impl(source=source, encoding=encoding, errors=errors,
62 header=header, **csvargs)
63
64
65 def fromtsv(source=None, encoding=None, errors='strict', header=None,
66 **csvargs):
67 """
68 Convenience function, as :func:`petl.io.csv.fromcsv` but with different
69 default dialect (tab delimited).
70
71 """
72
73 csvargs.setdefault('dialect', 'excel-tab')
74 return fromcsv(source, encoding=encoding, errors=errors, header=header, **csvargs)
75
76
77 def tocsv(table, source=None, encoding=None, errors='strict', write_header=True,
78 **csvargs):
79 """
80 Write the table to a CSV file. E.g.::
81
82 >>> import petl as etl
83 >>> table1 = [['foo', 'bar'],
84 ... ['a', 1],
85 ... ['b', 2],
86 ... ['c', 2]]
87 >>> etl.tocsv(table1, 'example.csv')
88 >>> # look what it did
89 ... print(open('example.csv').read())
90 foo,bar
91 a,1
92 b,2
93 c,2
94
95 The `source` argument is the path of the delimited file, and the optional
96 `write_header` argument specifies whether to include the field names in the
97 delimited file. All other keyword arguments are passed to
98 :func:`csv.writer`. So, e.g., to override the delimiter from the default
99 CSV dialect, provide the `delimiter` keyword argument.
100
101 Note that if a file already exists at the given location, it will be
102 overwritten.
103
104 """
105
106 source = write_source_from_arg(source)
107 csvargs.setdefault('dialect', 'excel')
108 tocsv_impl(table, source=source, encoding=encoding, errors=errors,
109 write_header=write_header, **csvargs)
110
111
112 Table.tocsv = tocsv
113
114
115 def appendcsv(table, source=None, encoding=None, errors='strict',
116 write_header=False, **csvargs):
117 """
118 Append data rows to an existing CSV file. As :func:`petl.io.csv.tocsv`
119 but the file is opened in append mode and the table header is not written by
120 default.
121
122 Note that no attempt is made to check that the fields or row lengths are
123 consistent with the existing data, the data rows from the table are simply
124 appended to the file.
125
126 """
127
128 source = write_source_from_arg(source, mode='ab')
129 csvargs.setdefault('dialect', 'excel')
130 appendcsv_impl(table, source=source, encoding=encoding, errors=errors,
131 write_header=write_header, **csvargs)
132
133
134 Table.appendcsv = appendcsv
135
136
137 def totsv(table, source=None, encoding=None, errors='strict',
138 write_header=True, **csvargs):
139 """
140 Convenience function, as :func:`petl.io.csv.tocsv` but with different
141 default dialect (tab delimited).
142
143 """
144
145 csvargs.setdefault('dialect', 'excel-tab')
146 return tocsv(table, source=source, encoding=encoding, errors=errors,
147 write_header=write_header, **csvargs)
148
149
150 Table.totsv = totsv
151
152
153 def appendtsv(table, source=None, encoding=None, errors='strict',
154 write_header=False, **csvargs):
155 """
156 Convenience function, as :func:`petl.io.csv.appendcsv` but with different
157 default dialect (tab delimited).
158
159 """
160
161 csvargs.setdefault('dialect', 'excel-tab')
162 return appendcsv(table, source=source, encoding=encoding, errors=errors,
163 write_header=write_header, **csvargs)
164
165
166 Table.appendtsv = appendtsv
167
168
169 def teecsv(table, source=None, encoding=None, errors='strict', write_header=True,
170 **csvargs):
171 """
172 Returns a table that writes rows to a CSV file as they are iterated over.
173
174 """
175
176 source = write_source_from_arg(source)
177 csvargs.setdefault('dialect', 'excel')
178 return teecsv_impl(table, source=source, encoding=encoding,
179 errors=errors, write_header=write_header,
180 **csvargs)
181
182
183 Table.teecsv = teecsv
184
185
186 def teetsv(table, source=None, encoding=None, errors='strict', write_header=True,
187 **csvargs):
188 """
189 Convenience function, as :func:`petl.io.csv.teecsv` but with different
190 default dialect (tab delimited).
191
192 """
193
194 csvargs.setdefault('dialect', 'excel-tab')
195 return teecsv(table, source=source, encoding=encoding, errors=errors,
196 write_header=write_header, **csvargs)
197
198
199 Table.teetsv = teetsv