简体   繁体   中英

How to read the text file format and write the same in to excel file as mentioned in the below format in python

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

  • 'adobe' in row 0, column 1
  • IE11.o to row 0, column 2
  • 32 to row 0, column 3

  • flash to row 1, column 1

  • IE8.0 to row 1, column 2
  • 24 to row 1, column 3

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM