Index de l'article

Advanced read in a TXT file

Work data in a TXT file according the line number

You can grab data in a TXT file according the line number, or counting a number of lines before or after a specific string.

file = open('C:/Users/Georges/Downloads/MyFile.txt', 'r')
lines = file.readlines()
listSubject = []
listFirstname = []
for num, x in enumerate(lines):
    if x.startswith('Subject:\t'):
MergeLists = list(zip(listSubject, listFirstname))
df = pd.DataFrame(MergeLists, columns=['field Subject', 'field Firstname'])

Get encoding of a file

from chardet import detect
def get_encoding_type(file):
    with open(file, 'rb') as f:
        rawdata =
    return detect(rawdata)['encoding']
from_codec = get_encoding_type(MyFile)


Liens ou pièces jointes
Télécharger ce fichier ([France-Departements-Deformation]335 Ko
Télécharger ce fichier ([simple_countries]1880 Ko