Hi, I have many text files with similar format as below.
Output: Apples: 1596.403 +- 10.651
Oranges: 10.164 +- 0.685
Bananas: 0.879 +- 0.200
Pears: 0.006 +- 0.047
Pears/Bananas: -0.021 +- 0.101I would like to import them into one excel spreadsheet with Apples, Oranges and Bananas as the column headers and the corresponding value from each file in the respective column like so:Output:Apples oranges Bananas
1596.403 10.164 0.879
1467.234 12.345 0.461
1234.456 14.658 0.592
....So on and so on.....Here is my code:It creates the spreadsheet "summary" with no contents.All suggestions would be greatly appreciated.Thank you.import pandas as pd
import numpy as np
import glob
txt_list = [] # empty list
sheet_list = [] # empty list
# a for loop through filenames matching a specified pattern (.txt) in the current directory
for infile in glob.glob("test-1.txt"):
outfile = infile.replace('.txt', '') #removing '.txt' for excel sheet names
sheet_list.append(outfile) #appending for excel sheet name to sheet_list
txt_list.append(infile) #appending for '...txt' to txtt_list
writer = pd.ExcelWriter('summary.xlsx', engine='xlsxwriter')
# a for loop through all elements in txt_list
for i in range(0, len(txt_list)):
df = pd.read_csv('%s' % (txt_list[i])) #reading element from txt_list at index = i
df.to_excel(writer, sheet_name='%s' % (sheet_list[i]), index=False) #reading element from sheet_list at index = i
writer.save()
Larz60+ write Apr-03-2021, 10:11 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time, please use bbcode tags on future posts.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time, please use bbcode tags on future posts.
