“Como colocar um arquivo de texto em uma lista Python” Respostas de código

Cada linha em um arquivo de texto em uma lista em Python

with open('file1.txt','r') as f:
	listl=[]
	for line in f:
		strip_lines=line.strip()
		listli=strip_lines.split()
		print(listli)
		m=listl.append(listli)
	print(listl)
Man

Como colocar um arquivo de texto em uma lista Python

# name.txt
david
mary
john


with open('names.txt', 'r') as f:
    myNames = [line.strip() for line in f]
    
# Result

['david','mary','john']
Shanti

Escreva o texto na lista no arquivo de texto python

all_sents = ['hey there', 'how are you']
with open('file_name.txt', 'w', encoding='utf-8') as f:
   f.write('\n'.join(all_sents)) 
NA RACE

Respostas semelhantes a “Como colocar um arquivo de texto em uma lista Python”

Perguntas semelhantes a “Como colocar um arquivo de texto em uma lista Python”

Procure respostas de código populares por idioma

Procurar outros idiomas de código