I am parsing a mustache file into a string, and after that I want to process that string with the csv module. For that I generate a file like interface to the string using StringIO. The csv module is complaining with:
_csv.Error: line contains NULL byte
So I made a simple test:
import pystache
from cStringIO import StringIO
txt = pystache.render('Hello {{name}}', {'name' : 'Steve'})
f = StringIO(txt)
data = f.read()
print txt.find('\x00')
print data.find('\x00')
print txt.count('\x00')
print data.count('\x00')
Which produces:
-1
1
0
33
Somehow the StringIO object is inserting NULL bytes. This does not happen if I use a string which has not been pre-processed with pystache:
from cStringIO import StringIO
txt = "Hello Steve"
f = StringIO(txt)
data = f.read()
print txt.find('\x00')
print data.find('\x00')
print txt.count('\x00')
print data.count('\x00')
The result is as expected:
-1
-1
0
0
What could the problem be?
txt = "Hello Steve"is a bytestring, could the preprocessed string be a unicode string?