【问题标题】:I have a text file with two parts. I want to split two parts into two lists. (python help)我有一个包含两部分的文本文件。我想将两个部分分成两个列表。 (蟒蛇帮助)
【发布时间】:2021-02-07 05:41:33
【问题描述】:

数据.txt

diuwhwhpjd 狩猎者 呸呸呸 例子呵呵 呜呜呜 徐峥嵘

示例 狩猎

我想在一个文件中从这些数据中创建两个列表。第一个列表将包含空行之前的所有数据,第二个列表将包含空行之后的单词。还有enter image description here

【问题讨论】:

    标签: python-3.x list file split


    【解决方案1】:

    请尝试以下代码:

    list1 = []
    list2 = []
    temp_list = []
    with open('data.txt', 'r') as f:
        for line in f.readlines():
            if not line.strip():
                list1 = temp_list
                temp_list = []
            else:
                temp_list.append(line.strip())
        list2 = temp_list
    

    希望这能解决您的问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 2021-04-28
      • 2013-11-12
      • 2011-11-26
      • 2011-07-19
      • 2011-09-24
      相关资源
      最近更新 更多