'disregard a line from txt file to excel file in python
I need to disregard lines when someone enters and leaves the channel as you can see in the below txt file.
7:52:01 AM sherr entered the channel
7:52:05 AM sherr
hello GOOD morning .
おはようございます。
7:52:09 AM sherr
Who ?
誰?
7:52:16 AM sherr
OK .
わかりました。
7:52:25 AM sherr left the channel.
7:52:32 AM gigi entered the channel
7:52:45 AM gigi
OK .
わかりました。
my code is supposed to output excel file that in every 3lines, they're in the same row and 1st line in 1st column, 2nd line in 2nd column and 3rd line in 3rd column. But I need to disregard the lines that has entered the channel and left the channel on it. what should I add? my codes looks like that as you can see below.
from openpyxl import Workbook
import copy
wb = Workbook()
with open('txtfile.txt', encoding='utf-8') as sherr:
row = 1
column = 1
ws = wb.active
for line in sherr:
if column == 1:
## split the line and rejoin
value = " ".join(line.strip().split(' ')[1:])
else:
value = line.strip()
ws.cell(row=row, column=column, value=value)
if (column := column + 1) > 3:
row += 1
column = 1
for row in ws.iter_rows():
for cell in row:
alignment = copy.copy(cell.alignment)
alignment.wrapText=True
cell.alignment = alignment
for column_cells in ws.columns:
length = max(len(str(cell.value)) for cell in column_cells)
ws.column_dimensions[column_cells[0].column_letter].width = length
wb.save('txt_to_exl.xlsx')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
