0

I'm trying to write this data:

 playlist =  {'playlist': {u'Up in Flames': 0, u'Oceans': 0, u'No Surprises': 0}}

to a file like so:

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

but it looks like writing integers to a file generator this error:

TypeError: expected a character buffer object

how do I correct this? Is there a better file format for my data structure?

1
  • playlist cast as string Commented Nov 6, 2016 at 3:38

3 Answers 3

2

You're trying to write a dictionary object to a text file, where as the function is expecting to get some characters to write. If you want your object to be stored in a text format that you can read, you need some way to structure your data, such as JSON.

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

There are other options such as xml or maybe even csv. If you don't care about your data being in a plain text readable format, you could also look at pickling the dictionary object.

As noted in the comments, your question appended data to a file, rather than writing a new file. This is an issue for JSON as the hierarchical structure doesn't work when its appended too. If you need to add to an existing file you may need to come up with a different structure for your stored text, read the exiting file, combine it with the new data and rewrite it (Jack Hughes answer)... or you could write some code to parse appended JSON, but I guess that's not the point of standards.

Sign up to request clarification or add additional context in comments.

6 Comments

Note that this will replace the contents of the file, not append them as in the example.
Can json.dump append to the file? Or just write?
It can append. Not sure that the resulting json file makes sense though thinking about it
the idea of having the integer as value is that every time a certain playlist is generated, I will have an incremental function to increment the value of preexisting key, or else append new keys and values to file. this will be later added to sqlite. which format do you recommend?
I would recommend going for SQLite early rather than giving yourself something else to go back and fix. databases exist to deal with all the issues you'll find trying to update a text file.
|
1
playlist =  {'playlist': {u'Up in Flames': 0, u'Oceans': 0, u'No Surprises': 0}}

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

or you can use json module:

with open('playlist.txt', 'w') as f:
    json.dump(playlist, f)

Comments

0

try this:

import json
playlist =  {'playlist': {u'Up in Flames': 0, u'Oceans': 0, u'No Surprises': 0}}
with open('playlist.txt', 'a') as f:
 json.dump(playlist, f)

It will probably work; however it might raise an error about not being able to write to the file. In this case you will have to change the a argument in the open statement, and you can't append only write to the file. Here's something to try to get around the problem:

import json
playlist =  {'playlist': {u'Up in Flames': 0, u'Oceans': 0, u'No Surprises': 0}}
with open('playlist.txt', 'r+') as f:
    playlist = playlist + json.dumps(f)
    json.dump(f, playlist) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.