[BangPypers] How should I do it?

Anand Balachandran Pillai abpillai at gmail.com
Fri Jan 15 11:47:05 CET 2010


On Fri, Jan 15, 2010 at 4:13 PM, Anand Chitipothu <anandology at gmail.com>wrote:

> On Fri, Jan 15, 2010 at 4:00 PM, Baishampayan Ghose <b.ghose at gmail.com>
> wrote:
> >> It is a clever hack, taking advantage of the nature of the data. But
> >> it is far more faster than the other approaches posted here.
> >
> > I thought eval was evil :)
>
> The date looks like valid json. You can use simplejson.loads instead of
> eval.
>
> Python 2.6.2 (r262:71600, Aug 21 2009, 12:23:57)
[GCC 4.4.1 20090818 (Red Hat 4.4.1-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import simplejson
>>> data=open('data.txt').read().replace('[code]','').replace('[/code]','')
>>> data
'\n"confident" => {\n count => 4,\n trans => {\n    "ashahvasahta" =>
0.74918568,\n   "atahmavaishahvaasa" => 0.09095465,\n   "pahraaram\\.nbha"
=> 0.06990729,\n        "mailatae" => 0.02856427,\n          "utanai" =>
0.01929341,\n            "anaa" => 0.01578552,\n        "uthaanae" =>
0.01403157,\n        "jaitanae" => 0.01227762,\n   },\n},\n"consumers" =>
{\n count => 4,\n trans => {\n   "upabhaokahtaa" => 0.75144362,\n
"upabhaokahtaaom\\.n" => 0.12980166,\n
"sauda\\\xef\xbf\xbd\\\xef\xbf\xbd\\\xef\xbf\xbddha" => 0.11875471,\n
},\n},\n"a" => {\n count => 1164,\n trans => {\n             "eka" =>
0.14900491,\n          "kaisai" => 0.08834675,\n            "haai" =>
0.06774697,\n            "kaoi" => 0.05394308,\n             "kai" =>
0.04981982,\n        "\\(none\\)" => 0.04400085,\n             "kaa" =>
0.03726579,\n             "kae" => 0.03446450,\n   },\n},\n\n'
>>> simplejson.loads(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/site-packages/simplejson/__init__.py", line
307, in loads
    return _default_decoder.decode(s)
  File "/usr/lib64/python2.6/site-packages/simplejson/decoder.py", line 338,
in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 13 - line 37 column 1 (char 13 - 815)


Anand
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
--Anand


More information about the BangPypers mailing list