Writing and reading EDN files (an extensible data notation format).
Code: "io_edn_file.py". Programming language: Python DMelt Version 1. Last modified: 03/13/2016. License: Pro
https://datamelt.org/code/cache/io_edn_file_1252.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.


 # This example shows how to write different objects in EDN files
 
from us.bpsm.edn.parser import Parsers 
from us.bpsm.edn.parser import Parser
from us.bpsm.edn.parser import Parseable
from us.bpsm.edn.printer import Printers
from us.bpsm.edn.parser.Parsers import defaultConfiguration;
from java.util import *
from java.io import *

sw = StringWriter()
p=Printers.newPrinter(sw)

print "Write a Java list"
al = ArrayList()
al.add(1)
al.add(2)
p.printValue(al)


print "Write Python dowble list"
s=[[22],[33,33]]
p.printValue(s)

print "Write a map"
map = HashMap()
map.put("name", 10000)
p.printValue(map)

print "Write a Python dictionary"
map={}
map["key1"]="VALUE1"
map["key2"]="VALUE2"
p.printValue(map)
data=sw.toString()
print "Data so far = ",data


print "Write this to text file"
fw = FileWriter("file.edn")
fw.write(data)
fw.close()


print "reading data back.."
pbr = Parsers.newParseable(data);
p = Parsers.newParser(defaultConfiguration());

a=p.nextValue(pbr)
print type(a), ":", a

a=p.nextValue(pbr)
print type(a), ":", a

a=p.nextValue(pbr)
print type(a), ":", a

a=p.nextValue(pbr)
print type(a), ":", a



Ads help maintain this website.