我想从下面的文本文件中读取数据并将其写入Excel表格,如进一步说明
使用新行上的每个值格式化文本文件。
Excel文件应该
32 到第0行第3列
闪存到第1行第1列
等等
答案 0 :(得分:0)
你可能想要read the text file, line by line, into a list。然后,您可以使用export your data as a .csv file来查看excel可以阅读的a library like Openpyxl,也可以直接将其作为Excel文件。
例如,如果您愿意生成.csv而不是excel文件,则此代码会执行您要求的内容:
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