views:

507

answers:

4

What type of Python objects should I use to parse files with a specific syntax? Also what sort of loop should be followed to make it through the file. Should one pass be sufficient? Two, three?

+1  A: 

how complex the syntax is? are you inventing a new one or not?

for a complex language, consider bison bindings like lex + pybison.

if you can decide what syntax to use, try YAML.

Francis
I'd suggest JSON to YAML, if only because it's in the standard library and YAML isn't.
Sii
+2  A: 

You should offer more information about your aims ...

  • What kind of file
  • What structure? Tab separated? XML - like?
  • What kind of encoding?
  • Whats the target structure?
  • Do you need to reparse the file in a regular time period (like an interpreter)?
Mario Mueller
A: 

It does not depend on your programming language (python) if your parser will have one, two, three or n passes. It depends on the grammar of the syntax you are trying to parse.

If the syntax is complex enough I would recommend LEX/YACC combo as Francis said.

Pablo Santa Cruz
+2  A: 

It depends on the grammar. You can use pyparsing instead of implementing your own parser. It is very easy to use.

Nadia Alramli