简体   繁体   中英

How to read integer values from a text file in python (digit by digit)

I have a text file in this format,

0
1
2 2 9
3 2 9
0
2
2 1 9
3 1 9
0
3
2 5 -6
4 1 -6
0
4
2 1 13
4 2 6

I have two Python Classes which are as follows,

class Timetable:
    def __init__(self):
        self.id = ""
        self.slot = []

class slots:
    def __init__(self):
        self.day = 0
        self.slot = 0
        self.room = 0

In my text file, 0 acts as the separator value, that is it separates different data from each other.

1
2 2 9
3 2 9

In this, 1 represents the "ID" of the class and the next two are the the slots of the this class in the timetable. In case of "2 2 9", it means that the first lecture of Class "1" will be held on "Tuesday, 2nd slot, in room number 9"

I want to store all this data in my classes.

For example, in this case, I want a Timetable object in which the ID will have "1".

and the slots will be an array of two objects of "slots". Each slot will have a day (in this case, it will be 2), a slot (in this case, it will be 2) and the room (in this case, it will be 9).

I am able to read the data, but I am unable to differentiate different data with each other. I can use the "0" as a separator to differentiate between two classes but I am unable to differentiate the two slots now.

In C++, it would have been very easy, ie I can easily hardcode it in such a way that first digit is read as the ID of class. Next three digits will create first object of slot and the next three will create second object of slot, and so on until all the file is read.

But, I am unable to make the logic for doing the same in Python. Can anyone kindly help me in doing this? Thank you.

You can hardcode in python as you do in C++. This way -

# Let f be file
# Assuming you know how to separate using seperator 0

# reads id. eg. id = 1
id = int(f.readline().strip()) 
# t1 stores list of int values. eg. t1 = [2, 2, 9]
t1 = list(map(int, f.readline().strip().split())) 
# t2 stores list of int values. eg. t1 = [3, 2, 9]
t2 = list(map(int, f.readline().strip().split())) 

See below

class Timetable:
    def __init__(self,id,slots):
        self.id = id
        self.slots = slots
    def __repr__(self) -> str:
        return f'Timetable: {self.id} {self.slots}'

class Slot:
    def __init__(self,day,slot,room):
        self.day  = day
        self.slot =  slot
        self.room = room
    def __repr__(self) -> str:
        return f' Slot: {self.day} {self.slot} {self.room}'
data = []
inside_tt = False
with open('data.txt') as f:
    lst = []
    for line in f:
        line = line.strip()
        if line == '0':
            if not inside_tt:
                inside_tt = True
            else:
                data.append(Timetable(id,lst))
                lst =[]
            next_is_id = True
            continue
        if inside_tt:
            if next_is_id:
                id = line
                next_is_id = False
            else:
                tmp = line.split()
                lst.append(Slot(tmp[0],tmp[1],tmp[2]))
data.append(Timetable(id,lst))
print(f'data: {data}')

output

data: [Timetable: 1 [ Slot: 2 2 9,  Slot: 3 2 9], Timetable: 2 [ Slot: 2 1 9,  Slot: 3 1 9], Timetable: 3 [ Slot: 2 5 -6,  Slot: 4 1 -6], Timetable: 4 [ Slot: 2 1 13,  Slot: 4 2 6]]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM