Python readline() Question.

Asked 2 years ago, Updated 2 years ago, 15 views

while True:

           Bus_Data = f.readline()
           Bus_line = Bus_Data.split(',')
           Bus_Dict = {}

           if len(Bus_line) == 2 :
               break
           print(Bus_line)

I used the while statement to get the variable for each value that is gathered there. I want to express the problem here as a different variable (bus_line1 and bus_line2 for each line, not just one bus_line). What should I do?

python

2022-09-21 18:23

1 Answers

To meet requirements such as questioners, that is, to dynamically generate variable names You have to process it using reflection.

However, reflection is not a good solution in the questioner's code.

It is difficult to write down specific answers because there are only a few codes on the question, but if the amount of data is small, it is better to receive them all in the list below and use them.


with open("file.txt") as f:
    Bus_Data_list = [Bus_Data.split(',') for Bus_Data in f]

Bus_Data_list[0] #First
Bus_Data_list[1] #Second


2022-09-21 18:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.