0

I have a file which contain entries in the following format:

FID=COST|Tolerance=1

FID=(BUY,SELL)|Tolerance=0

FID=[(X,Y),(A,B)]|Tolerance=0

What would be the best way to find out if FID is a string, tuple or list while reading the file?

A normal isinstance test of the value of FID immediately after reading wont work. Appreciate any help.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
IUnknown
  • 9,301
  • 15
  • 50
  • 76
  • 2
    If you're reading the file with basic file operations (rather than some kind of parsing library), you'll always be getting strings. Are you asking about how to parse the strings to get actual lists or tuples? – Blckknght Apr 23 '13 at 07:10
  • I am fine with a standard library like configparser too - anything that solves this in the most efficient way,without needing to code raw character parsing routines. – IUnknown Apr 23 '13 at 07:56

1 Answers1

0

Set A,B,X,Y then use eval

try:
   FID=eval("[(X,Y),(A,B)]")
 except SyntaxError:
   print "unknown variable or syntax error"
 if (isinstance(FID, tuple)):
   print "It's a tuple!!"

same kind of approach can be used for other types

Vorsprung
  • 32,923
  • 5
  • 39
  • 63