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?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198

3 Answers3

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.

Community
  • 1
  • 1
user5219763
  • 1,284
  • 12
  • 19
  • Note that this will replace the contents of the file, not append them as in the example. – jath03 Nov 06 '16 at 03:43
  • Can `json.dump` append to the file? Or just write? – jath03 Nov 06 '16 at 03:45
  • It can append. Not sure that the resulting json file makes sense though thinking about it – user5219763 Nov 06 '16 at 03:46
  • 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? – 8-Bit Borges Nov 06 '16 at 03:56
  • 1
    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. – user5219763 Nov 06 '16 at 04:14
  • it will go to `SQLite` from here. the point is this data structure is tailored for `collaborative filtering` `dictionaries` based on `ranks`, or `counts`, as well. the idea is have the same structure serve both `sqlite` and `collaborative filtering` `functions`. – 8-Bit Borges Nov 06 '16 at 04:22
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)
Januka samaranyake
  • 2,385
  • 1
  • 28
  • 50
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) 
jath03
  • 2,029
  • 2
  • 14
  • 20