jeudi 5 novembre 2015

Having trouble sorting the data from a file

I am opening and reading a .txt file and trying to save values in the format 0.x or 0.xy or 0.xyz.

x must be a digit 1-9

y cannot be 0 or a odd number

z cannot be 0 or a even number

My current code is only saving the variables in the format 0.x but is skipping over the 0.xy and 0.xyz.

For text file has 16000 elements and contains ints, floats, and strings:

.03243234

234.234

.223

0.2

MWFE

etc.

list = []

with open("exam2data.txt") as f:
    for line in f:
        line = f.readline()
        xCounter = 0
        yCounter = 0
        zCounter = 0

        try:
            lineFloat = float(line)
            if lineFloat < 1:
                if len(line) == 4:
                    if line[3] == 0:
                        pass
                    else:
                        list.append(lineFloat)
                        xCounter += 1

                elif len(line) == 5:
                    if line[3] == 0:
                        pass
                    else:
                        if line[2] == 0:
                            pass
                        else:
                            y = float(line[4])
                            if (y % 2 == 0):
                                list.append(lineFloat)
                                yCounter += 1
                            else:
                                pass

                elif len(line) == 6:
                     if line[4] == 0:
                        pass
                     else:
                        if line[5] == 0:
                            pass
                        else:
                            if line[3] == 0:
                                pass
                            else:
                                y = float(line[4])
                                z = float(line[5])
                                if (y % 2 == 0):
                                    if (z % 2 == 1):
                                        list.append(lineFloat)
                                        zCounter += 1
                                    else:
                                        pass
                                else:
                                    pass


        except:
            pass

print(len(list))
print(', '.join(map(str, list)))

Aucun commentaire:

Enregistrer un commentaire