jeudi 24 décembre 2020

Select random line in txt file with Python

I want to use Python to output a random line from an external .txt file. In this .txt file there are several sentences. But each of them is in a different line.

My approach is to generate a random line number:

import random

line = random.randint(1, max_line)

#max_line stands for the number of lines in the .txt file.

and then to reproduce this line using print().

I've already looked around a bit, but haven't found anything yet regarding outputting the sentence of the line. Any idea how I could make this work?




Aucun commentaire:

Enregistrer un commentaire