write python object __str__ to file -
class c: pass file = open("newfile.txt", "w") j in range(10): c = c() print c file.write(c) file.close()
is there wrong in code?
new python , want write content that's outputted 'print c'
file ?
you can use str()
function convert object string same way print
does:
for j in range(10): c = c() print c file.write(str(c))
this not include newline, however. if need newline well, can manually add one:
file.write(str(c) + '\n')
or use string formatting:
file.write('{}\n'.format(c))
or use print
statement redirection (>> fileobject
):
print >> file, c
Comments
Post a Comment