Python 如何将包含两列字符串的txt拆分为一个列表?

Python 如何将包含两列字符串的txt拆分为一个列表?,python,string,list,Python,String,List,我需要一些简单的帮助!我尝试过不同的代码,但都没有正常工作 我有一个.txt文件,两列之间用空格隔开。该文件如下所示: 我想将这些字符串拆分为一个列表,以获得以下结果: my_list=['1', 'abacaxi','1','abalo','1','abalos', '0', 'abacate'] 我该怎么做?下面的代码运行,但结果不是我需要的 import os import io import sys from pathlib import Path while True:

我需要一些简单的帮助!我尝试过不同的代码,但都没有正常工作

我有一个.txt文件,两列之间用空格隔开。该文件如下所示:

我想将这些字符串拆分为一个列表,以获得以下结果:

my_list=['1', 'abacaxi','1','abalo','1','abalos', '0', 'abacate']
我该怎么做?下面的代码运行,但结果不是我需要的

import os
import io
import sys
from pathlib import Path

while True:
    try:
        file_to_open =Path(input("Please, insert your file path: "))
        with open(file_to_open,'r', encoding="utf-8") as f:
            words = f.read().lower()
            break         
    except FileNotFoundError:
        print("\nFile not found. Better try again")
    except IsADirectoryError:
        print("\nIncorrect Directory path.Try again")


print('total number of words + articles: ', len(words))
corpus=words.split(' ')
print(corpus[0:20])
给你

with open(file_to_open,'r', encoding="utf-8") as f:
    words = f.read().lower()

#Split the lines and join them into one line, and single spaces between them
words = " ".join(words.split(sep='\n'))

#remove double spaces with single space
    while "  " in words:
        words = words.replace("  ", " ")
#Split the line silimiter ' ' i.e. space into a list
word_li = " ".join(words.split(sep=' '))

这回答了你的问题吗?您的示例的输出是什么?输出应该是这样的:my_list=['1','abacaxi','1','abalo','1','abalos','0','abacate']@mkrieger1,此代码不起作用。我希望得到以下结果:my_list=['1','abacaxi','1','abalo','1','abalos','0','abacate']