SoFunction
Updated on 2024-11-10

Solving the problem of encountering empty lines when converting python file strings to lists

The contents of the document are as follows:

Alex 100000
Rain 80000
Egon 50000
Yuan 30000
        #There is a blank line here!

Now see how it is processed and turned into a list!

salary_info = open("", "r+", encoding="UTF-8")
salary_info_list = []
for line in salary_info.readlines():
  if line == '\n':
    pass
  else:
    salary_info_list.append(list(())) # The read list is appended as an append to thesalary_listnumerical value。

The output is as follows:

[['Alex', '100000'], ['Rain', '80000'], ['Egon', '50000'], ['Yuan', '30000']]

The above article to solve the problem of encountering empty lines when python file string to list is all that I have shared with you, I hope to give you a reference, and I hope you will support me more.