Index de l'article

Advanced read in a TXT file

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'])