lundi 11 janvier 2021

if statement acting wrong

I have input like this:

byr:1983 iyr:2017
pid:796082981 cid:129 eyr:2030
ecl:oth hgt:182cm

iyr:2019
cid:314
eyr:2039 hcl:#cfa07d hgt:171cm ecl:#0180ce byr:2006 pid:8204115568

byr:1991 eyr:2022 hcl:#341e13 iyr:2016 pid:729933757 hgt:167cm ecl:gry

hcl:231d64 cid:124 ecl:gmt eyr:2039
hgt:189in
pid:#9c3ea1

ecl:#1f58f9
pid:#758e59
iyr:2022
hcl:z
byr:2016 hgt:68 eyr:1933

hcl:#fffffd ecl:gry eyr:2022
hgt:172cm pid:781914826 byr:1930 iyr:2018

hcl:#08df7e ecl:grn byr:1942
eyr:2028 iyr:2011 cid:141 pid:319110455
hgt:186cm

and so on +250 packages(package - set of byr,iyr... separated by empty line) like that.

My code is like this:

result_file = open('results.txt', 'w')

with open('input.txt', 'r') as f:
    for line in f:
        if line[:3] == 'cid':
            continue
        elif line == "\n":
            result_file.write('\n')
        else:
            line = line.replace('\n','')
            for i in line.split(' '):
                result_file.write(i + '\n')

result_file.close()

and it turns my input into this structure:

byr:1983
iyr:2017
pid:796082981
cid:129
eyr:2030
ecl:oth
hgt:182cm

iyr:2019
eyr:2039
hcl:#cfa07d
hgt:171cm
ecl:#0180ce
byr:2006
pid:8204115568

byr:1991
eyr:2022
hcl:#341e13
iyr:2016
pid:729933757
hgt:167cm
ecl:gry

hcl:231d64
cid:124
ecl:gmt
eyr:2039
hgt:189in
pid:#9c3ea1

ecl:#1f58f9
pid:#758e59
iyr:2022
hcl:z
byr:2016
hgt:68
eyr:1933 

but the problem is that line if line[:3] == 'cid': continue seems to be working only 'sometimes'(however strange it might sound), and I want to have file like above, but without any cid:value fields.

As always, my reasoning(solution in code) seems to be correct for me, and I can't find out where the mistake is.

Please help. Thanks from advance!

Aucun commentaire:

Enregistrer un commentaire