Fixed an python file string rotation problem with empty lines

  • 2020-06-12 09:33:46
  • OfStack

The file contents are as follows:


Alex 100000
Rain 80000
Egon 50000
Yuan 30000
        # Here are 1 A blank line! 

Now let's see how to do that and turn it into a list!


salary_info = open("salaryinfo.txt", "r+", encoding="UTF-8")
salary_info_list = []
for line in salary_info.readlines():
  if line == '\n':
    pass
  else:
    salary_info_list.append(list(line.split())) #  The list that you read is appended to salary_list In the value. 

The output results are as follows:


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

Related articles: