0

I am using Jython to script for a test framework (jython is the language), but I'm hoping it is a generic question for Jython/Python.

I am trying to read each line from a file into a tuple which is then worked on. I am reading the line like so:

tupleUser = ast.literal_eval(anInterestingString)

This what the 'interesting string variable' looks like:

(1,SECOND_FIELD,242166347.d135e34.9c90e786e7ae4175bfdb08028969d567)

The code throws this error:

aborting process - Jython exception, : no viable alternative at input 'd135e34' (, line 1) [initialising test script] net.grinder.scriptengine.jython.JythonScriptExecutionException: : no viable alternative at input 'd135e34' (, line 1)

I am just going to try to parse this and use the values, but I would like to learn how to use 'ast' to parse things like this. It would be much easier.

Bill Rosmus
  • 2,941
  • 7
  • 40
  • 61
  • 4
    It's invalid Python code, so you can't parse it with `ast`. – Blender Aug 16 '13 at 19:41
  • Well, you can parse it, you just can't parse it to Python values. – Ignacio Vazquez-Abrams Aug 16 '13 at 19:44
  • @Blender care to elaborate? If there are no periods it works. There are several answers on Stack that say this is the preferred way to do this. If it isn't valid, care to show why or show how? http://stackoverflow.com/questions/14611352/malformed-string-valueerror-ast-literal-eval-with-string-representation-of-tup http://stackoverflow.com/questions/4832789/read-in-tuple-of-lists-from-text-file-as-tuple-not-string-python Those are two... there are more. Please tell them this is invalid. – Bill Rosmus Aug 16 '13 at 20:28
  • @BillR its validity is based on the source data and the purpose to which you wish to put it... This isn't suitable for `ast.literal_eval` and using anything else in the `ast` module would be overkill – Jon Clements Aug 16 '13 at 20:33
  • @JonClements so the better answer is that the ast module can't handle periods in a string. It's OK, I just did it myself. Thanks – Bill Rosmus Aug 16 '13 at 20:38
  • 1
    The reason that ast.literal_eval cannot "handle" periods in a string is that anInterestingString does not contain a valid python expression, as @Blender said. If there were quotes around the third component, it would be fine. From the docs, "The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None." – yotommy Aug 16 '13 at 21:16
  • @BillR: If it was valid Python, you *could* parse it with `ast.parse`, but it isn't. `242166347.d` would have to be `(242166347).d` and `.9c9` wouldn't work, as the property name starts with a number. – Blender Aug 17 '13 at 02:47
  • @Blender The thing is that this: "242166347.d135e34.9c90e786e7ae4175bfdb08028969d567", is a string. I am trying to parse three fields in a tuple into a tuple variable. Each line in the file is like that, with either three or four items in each tuple (i.e. one tuple per line in the file). And the code, I think, is OK. It is just that ast can't parse a string with periods in it. It is a limitation of the library and I'm OK with that as long as I know it is that. I did try putting that one string in quotes, and each line in quotes, but got more errors so said screw it and parsed it myself. – Bill Rosmus Aug 18 '13 at 18:02

0 Answers0