The interface for fileobj.write()
requires that you write a string, always:
file.write(str)
Write a string to the file.
Emphasis mine.
It is an implementation detail that for StringIO()
non-strings just happen to work. The code optimises the 'empty string' case by using:
if not s: return
to avoid doing unnecessary string concatenation. This means that if you pass in any falsey value, such as numeric 0 or None
or an empty container, writing doesn't take place.
Convert your objects to a string before writing:
for v in a:
stream.write(str(v))
stream.write('\n')
If you used the C-optimised version here you'd have had an error:
>>> from cStringIO import StringIO
>>> f = StringIO()
>>> f.write(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: must be string or buffer, not int