I wrote a python program to print the each line of gedcom file with its level no and tag( gedcom is a file that is basically a family tree).
Each line from gedcom has a structure as follows
<level-number> <tag> <arguments>
Now I do not want all tags to be printed but only specific tags which i added in key_words list and for the rest i want to print "invalid tag". Now the problem is the " invalid tag" is get printed every time even though matching tag is found and get printed. Basically if statement is executed every time .
How can i fix this problem? also how can i deal with the 'INDI' word as it is not getting printed
here is my code
key_words = ['INDI','NAME','SEX','BIRT','DEAT','FAMC','FAMS','FAM','MARR','HUSB','WIFE','CHIL','DIV','DATE','HEAD','TRLR','NOTE']
#opening file
text_file = open('C:\Users\shree\Canopy\My-Family-18-May-2016-582.ged', 'r')
print "Printing each line of gedcom file followed by level no and tag line"
for line in text_file:
print "line is:-", line
level_number = int(line[:1])
print "Level number is",level_number
line = line.split()
for word in key_words:
if word in line:
print "Tage is:-",word,"\n"
else:
print "invalid tag"
sample lines
0 HEAD
1 SOUR Family Echo
2 WWW http://www.familyecho.com/
1 FILE My Family
1 DATE 18 MAY 2016
1 DEST ANSTFILE
1 GEDC
2 VERS 5.5.1
2 FORM LINEAGE-LINKED
1 SUBM @I1@
2 NAME Nico Rosberg
1 SUBN
1 CHAR UTF-8
0 @I1@ INDI
1 NAME Nico /Rosberg/
2 GIVN Nico
2 SURN Rosberg
2 _MARNM Rosberg
1 SEX M
1 BIRT
2 DATE 21 MAR 1989
1 FAMC @F1@
0 @I2@ INDI
1 NAME Tom /Rosberg/
2 GIVN Tom
2 SURN Rosberg
2 _MARNM Rosberg
1 SEX M
1 BIRT
2 DATE 15 MAR 1958
1 FAMS @F1@
1 FAMC @F2@
0 @I3@ INDI
1 NAME Laisly /Vettle/
2 GIVN Laisly
2 SURN Vettle
2 _MARNM Rosberg
1 SEX F
1 BIRT
2 DATE 15 SEP 1958
1 FAMS @F1@
1 FAMC @F3@
It seems what you want is this:
line_words = line.split()
# get the first element since that is the tag of line
line_tag = line_words[1].strip()
# check if that is present in the keywords
if line_tag in key_words:
print "Tag is:-",line_tag,"\n"
else:
print "invalid tag"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.