python - csv class writer : a bytes-like object is required, not 'str' -
for personal project i'm trying upgrade paterns package python 3. i'm running test:db.py, i'm stuck following error in '__init__.py' file, on csv class:
this code snippet od save() function: there, dfine s bytesio() stream, function asked stream bytes self csv file. error comes line:
w.writerows([[csv_header_encode(name, type) name, type in self.fields]]) typeerror: bytes-like object required, not 'str' ( below, code function)
it's supposed csv_header_encode deliver bytes, , checked , does, somehow, in conversion list changes 'str'. , if change s encoding stringsio complaining comes from
f.write(bom_utf8)
any appreciated.
def save(self, path, separator=",", encoder=lambda v: v, headers=false, password=none, **kwargs): """ exports table unicode text file @ given path. rows in file separated newline. columns in row separated given separator (by default, comma). data types other string, int, float, bool or none, custom string encoder can given. """ # optional parameters include arguments csv.writer(), see: # http://docs.python.org/library/csv.html#csv.writer kwargs.setdefault("delimiter", separator) kwargs.setdefault("quoting", csvlib.quote_all) # csv.writer handle str, int, float , bool: s = bytesio() w = csvlib.writer(s, **kwargs) if headers , self.fields not none: w.writerows([[csv_header_encode(name, type) name, type in self.fields]]) w.writerows([[encode_utf8(encoder(v)) v in row] row in self]) s = s.getvalue() s = s.strip() s = re.sub("([^\"]|^)\"none\"", "\\1none", s) s = (s if not password else encrypt_string(s, password)).encode('latin-1') f = open(path, "wt") f.write(bom_utf8) f.write(s) f.close() def csv_header_encode(field, type=string): # csv_header_encode("age", integer) => "age (integer)". t = re.sub(r"^varchar\(.*?\)", "string", (type or "")) t = t , " (%s)" % t or "" return "%s%s" % (encode_utf8(field or ""), t.upper())
you may trying write bytesio
object, csv.writer()
deals in strings only. csv
writer objects documentation:
a row must iterable of strings or numbers
emphasis mine. csv.writer()
requires text file write to; object produces strings:
[...] converting user’s data delimited strings on given file-like object.
either use io.stringio
object instead, or wrap bytesio
object in io.textiowrapper
object handle encoding you. either way, you'll need pass in unicode text csv.writer()
.
because later on treating s.getvalue()
data strings again (using regular expressions defined strings, , using encoding latin-1), want write text file (so stringio
).
that f.write(bom_utf8)
fails separate issue. f
opened in text mode ('wt'
) expects strings, not bytes
. if want write text file encoded utf-8 utf-8 bom @ start can use utf-8-sig
encoding when opening file:
open(path, 'w', encoding='utf-8-sig')
generally, appear mixing bytes , strings in wrong ways. leave text as text long possible, , encode @ last possible moment. here moment when writing file @ location path
, , can leave encoding entirely file object.
Comments
Post a Comment