[英]Python split tabspaced bilingual txt to two separate txt files (list) with newlines separating strings
[英]How to split a txt with two columns of strings into a list?
我需要一些简单的帮助,我尝试了不同的代码。 但没有任何工作正常。
我有一个 .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])
这里是 go,
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=' '))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.