Page 7 sur 25
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'): listSubject.append(x) listFirstname.append(lines[num+6]) 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 = f.read() return detect(rawdata)['encoding'] from_codec = get_encoding_type(MyFile) print('from_codec') print(from_codec)
Read a txt file containing a comma separated list of numbers and make a list in python
with open(pluImport + '\\' + file, 'r') as myFile: fileContent = myFile.read() # You can add some formating if needy # fileContent = myFile.read().replace("'", '').replace('"', '') myList = [int(myNumber) for myNumber in fileContent.split(',')]