Reputation: 1
I have to put some numerical data, which I received in text file into an Excel file (no, Pandas won't work, because of the way that data is written in the file) and for some reason, it just won't work it just won't work. My latest attempt to get it done is below. I also tried with openpyxl
with no success.
wb = w.Workbook('D:\\IJS\\JEFF444.xlsx')
ws = wb.add_worksheet()
with open(trimmed_data, 'r') as file:
lines = file.readlines()
row = 8
for index in range(len(lines)):
if len(lines[index].split()) != 0:
lst = lines[index].split()
print(lst)
if lst[-1][-3:] == '444':
print(lst[3].split('/')[0])
ws.write(f'B{row}', lst[3].split('/')[0])
ws.write(f'E{row}', float(lines[index + 2].split()[1]))
ws.write(f'F{row}', float(lines[index + 2].split()[1]) * float(lines[index + 2].split()[2]))
ws.write(f'J{row}', float(lines[index + 5].split()[1]))
ws.write(f'K{row}', float(lines[index + 5].split()[1]) * float(lines[index + 5].split()[2]))
ws.write(f'O{row}', float(lines[index + 8].split()[1]))
ws.write(f'P{row}', float(lines[index + 8].split()[1]) * float(lines[index + 8].split()[2]))
ws.write(f'T{row}', float(lines[index + 11].split()[1]))
ws.write(f'U{row}', float(lines[index + 11].split()[1]) * float(lines[index + 11].split()[2]))
row += 1
file.close()
wb.close()
Upvotes: 0
Views: 124
Reputation: 33
Excel can read .csv Files wich you can create with the integrated Library csv from Python. If you don't have to use .xlsx maybe this could be the way to go. Here you can learn how to use it: https://realpython.com/python-csv/
There are tons of Tutorials out there on YouTube if you still need help with csv.
Hopefully this can help you :)
Upvotes: 1