Writing a set to an output file in python -


i use json lists, doesn't work sets. there similar function write set output file,f? this, sets:

f=open('kos.txt','w') json.dump(list, f) f.close() 

json not python-specific format. knows lists , dictionaries, not sets or tuples.

but if want persist pure python dataset use string conversion.

with open('kos.txt','w') f:    f.write(str({1,3,(3,5)}))  # set of numbers & tuple 

then read again using ast.literal_eval

import ast open('kos.txt','r') f:    my_set = ast.literal_eval(f.read()) 

this works lists of sets, nested lists sets inside... long data can evaluated literally. serializing python basic object structure str can parsed it.

you have used pickle module, creates binary data, more "opaque", , there's way use json: python sets not json serializable. needs, stick str/ast.literal_eval


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -