I want to read the data from the text file below and write it to an Excel sheet as explained further
The text file is formatted with each value on a new line.
The Excel file should have
32 to row 0, column 3
flash to row 1, column 1
and so on for many rows
You probably want to read the text file, line by line, into a list . Then you can either export your data as a .csv file which excel can read, or directly as an excel file using a library like Openpyxl .
For example this code does what you've asked for, if you're happy to produce a .csv instead of an excel file:
fname = "" #path to file
csvname = "" #path to output csv
with open(fname) as f: #reads the file
content = f.readlines() #writes lines to list
content = [x.strip() for x in content] #removes blank list values
names = content[0::3] #builds list of names
IEs = content[1::3] #builds list of IEs
numbers = content[2::3] #builds list of numbers
with open(csvname) as file: #creates csv file
for i in range(len(content)/3): #runs through rows
file.write(names[i]+","+IEs[i]+","+numbers[i]) #writing data to rows
file.write('\n') #ends line after each row
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.