jeudi 7 janvier 2016

if x == '' wont register when reading from file

Trying to get my program to split lines into 3 rows from a file and then apply a "if row1 == x:" to add to an existing class. Now thats not my problem, ive gotten it to work, except for when row1 is ''. So i tried changing the input file so it was ' ', then '*', and 'k' (and so on), nothing worked. Thing is that most lines in the input file reads: 1234565,'streetadress1','streetadress2' but for some lines there are no streetadress1 only ''. but the program has no problem identifying the number or 'streetadress2'.

class adress(object):
def __init__(self,street,ykord,xkord):
    self.street = street
    self.ykord = ykord
    self.xkord = xkord
    self.connected = []
    self.anlid = []
    self.distances = []
    self.parent = []
    self.child =[]


def set_connections(self):
    input_file = open("kopplingar2.txt")
    temp = input_file.read().splitlines()
    for l in temp:
        row = l.split(',')
        identity = row[0]
        streetA = row[1]
        streetB = row[2]
        if streetA == self.street:
            diction = {'street':streetB, 'identity':identity}
            self.child.append(diction)
        elif streetA == '':
            self.anlid.append(identity)
            print 'poop!'
        elif streetB == self.street and streetA != '':
            diction = {'street':streetA, 'identity':identity}
            self.parent.append(diction)
            print streetA

The 'print poop' is just to see if it ever occur, but it doesnt. It should be about 400 lines of poop as a result since about 75% of the lines in the inputfile contain ''.

I have no idea why its working for the other rows but not for row1 (except that it sometimes is '' instead of a full string).

Aucun commentaire:

Enregistrer un commentaire