I would like to write a list to a file and read back the contents of the file into a list.
I am able to write the list to the file using simplejson as follows:
f = open("data.txt","w")
l = ["a","b","c"]
simplejson.dump(l,f)
f.close()
Now to read the file back i do
file_contents = simplejson.load(f)
But, i guess file_contents is in json format. Is there any way to convert it to a list ?
Thank You.
is indeed reloading the data exactly as you specify. What may be confusing you is that all strings in JSON are always Unicode — JSON (like Javascript) doesn’t have a “byte string” data type distinct from “unicode”.
Edit I don’t have the old
simplejsonaround any more (since its current version has become part of the standard Python library asjson), but here’s how it works (makingjsonmasquerade assimplejsonin the hope of avoiding confusing you!-)…:If this exact code (net of the first two lines if what you do instead is
import simplejsonof course;-) doesn’t match what you observe, you’ve found a bug, so it’s crucial to report what versions of Python andsimplejsonyou’re using and exactly what error you get, complete with traceback (edit your Q to add this — obviously crucial — info!).