fwrite - Python - TypeError: expected a character buffer object -


i'm trying write data:

 playlist =  {'playlist': {u'up in flames': 0, u'oceans': 0, u'no surprises': 0}} 

to file so:

 open('playlist.txt', 'a') f:      f.write(playlist) 

but looks writing integers file generator error:

typeerror: expected character buffer object

how correct this? there better file format data structure?

you're trying write dictionary object text file, function expecting characters write. if want object stored in text format can read, need way structure data, such json.

import json open('playlist.json', 'w') f:     json.dump(playlist, f) 

there other options such xml or maybe csv. if don't care data being in plain text readable format, @ pickling dictionary object.

as noted in comments, question appended data file, rather writing new file. issue json hierarchical structure doesn't work when appended too. if need add existing file may need come different structure stored text, read exiting file, combine new data , rewrite (jack hughes answer)... or write code parse appended json, guess that's not point of standards.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -