python - Comment lire un fichier ligne par ligne dans une liste

Mots clés : pythonstringfilereadlinespython

meilleur 5 Réponses python - Comment lire un fichier ligne par ligne dans une liste

vote vote

97

with open(filename) as file:     lines = file.readlines()     lines = [line.rstrip() for line in lines] 
with open(filename) as file:     for line in file:         print(line.rstrip()) 
with open(filename) as file:     while (line := file.readline().rstrip()):         print(line) 
with open(filename, 'r', encoding='UTF-8') as file:     while (line := file.readline().rstrip()):         print(line) 
vote vote

89

with open('filename') as f:     lines = f.readlines() 
with open('filename') as f:     lines = [line.rstrip() for line in f] 
vote vote

76

with open("file.txt") as file_in:     lines = []     for line in file_in:         lines.append(line) 
vote vote

62

lines = tuple(open(filename, 'r')) 
vote vote

60

with open(fname) as f:     content = f.readlines() 
with open(fname) as f:     content = f.read().splitlines() 

Questions similaires