By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

How to fix invalid syntax else error?

P: 2
This is the first time I am attempting this - someone was kind enough to help me with my previous error and told me to add print statements before the error which I did the error is now invalid syntax else: on line 130
Expand|Select|Wrap|Line Numbers
  1. class BadLine(Exception):
  2.     pass
  3.  
  4. def isblank(item):
  5.     return len(item) == 0 or item.isspace()
  6.  
  7. def triad(key,data):
  8.     try:
  9.         assert key in fields
  10.     except:
  11.         print "key %s not found" %key
  12.         raise ValueError,key
  13. print data
  14. print fields2
  15. print key
  16.  
  17. item = data[fields2[key]]
  18. if key in noblanks:
  19.         item = item.replace(" ","")
  20. if key.startswith("_"):
  21.         key = key.replace("_","")    # hack for double column names
  22. if isblank(item):
  23.         if defaults:
  24.             if key in defaults.keys():
  25.                 return  "<%s>"%key+defaults[key]+"</%s>"%key
  26.             else:
  27.                 return  "<%s>"%key+"(inconnu)</%s>"%key
  28.             else:
  29.             print line
  30.             raise BadLine
  31.                 return "<%s>"%key+item+"</%s>"%key
  32.  
  33. defaults = {"NAS":"000000000","A_DateNaissance":"1/1/20","B1_NbJoursGarde":"0"}
  34. noblanks = ["NAS","CodePostal"]
  35.  
  36.  
  37. filename = "KR.tsv"
  38. outname = "KR5.xml"
  39.  
  40. outfile = file(outname,"w")
  41.  
  42. data = [item.strip().split("\t") for item in file(filename).readlines()]
  43. fields = data[0]
  44. fields2 = dict([(item[1],item[0]) for item in enumerate(fields)])
  45.  
  46. # hack for double column names
  47. fields2["_Prenom"]=4
  48. fields2["_NomFamille"]=3
  49.  
  50. fields += "_Prenom _NomFamille".split()
  51. # end hack for double column names
  52.  
  53. print fields2
  54.  
  55. outfile.write(prefix)
  56.  
  57. for line in data[1:]:
  58.     if not [item for item in line if (not isblank(item))]:
  59.         break     # TSV file ends with many blank lines; exit and clean up on first one
  60.     try:
  61.         record = ["\n\n<R>"]
  62.  
  63.         record.append(triad("Annee",line))
  64.         record.append(triad("NoReleve",line))
  65.  
  66.         record.append("<Payeur>")
  67.  
  68.         record.append(triad("NAS",line))
  69.         record.append(triad("_NomFamille",line))
  70.         record.append(triad("_Prenom",line))
  71.  
  72.         record.append("<Adresse>")
  73.         record.append(triad("Ligne1",line))
  74.         record.append(triad("Ville",line))
  75.         record.append(triad("Province",line))        
  76.         record.append(triad("CodePostal",line))
  77.         record.append("</Adresse>")
  78.  
  79.         record.append("</Payeur>")
  80.  
  81.         record.append("<Enfant>")
  82.  
  83.         record.append(triad("NomFamille",line))
  84.         record.append(triad("Prenom",line))
  85.         record.append(triad("A_DateNaissance",line))
  86.  
  87.         record.append("<CaseB>")
  88.         record.append(triad("B1_NbJoursGarde",line))
  89.         record.append("</CaseB>")
  90.  
  91.         record.append("<Montants>")
  92.         record.append(triad("C_FraisPayes",line))
  93.         record.append("</Montants>")
  94.  
  95.         record.append("</Enfant>")
  96.  
  97.         record.append("</R>") 
  98.         record = "\n".join(record)
  99.         outfile.write(record)
  100.     except BadLine:
  101.         pass
  102.  
  103.  
  104. outfile.write(suffix)
  105. outfile.close()
  106.  
Feb 2 '11 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
This if/else block is invalid:
Expand|Select|Wrap|Line Numbers
  1.             if key in defaults.keys():
  2.                 return  "<%s>"%key+defaults[key]+"</%s>"%key
  3.             else:
  4.                 return  "<%s>"%key+"(inconnu)</%s>"%key
  5.             else:
  6.             print line
Can you see it?
Feb 2 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.