Hello,
I am using python 3.8.2
I have a large text file whereby I am trying to write the lines which starts with & until it finds a line in the file that starts with @ then. write that block of lines to a cell in excel. And the code should continue until the end of the text file searching for & and @ and writing to my excel file.
Interesting the output from my print looks right. If I could just get each block of lines in an excel that would be great.
import xlsxwriter
# Create an xlsx file
workbook = xlsxwriter.Workbook(outfile)
# Format the .xlsx
bold = workbook.add_format({'bold': True})
worksheet = workbook.add_worksheet()
#
# Create Workbook
worksheet.write('A1', 'Name', bold)
worksheet.write('B1', 'Status', bold)
worksheet.write('C1', 'TOS Query', bold)
worksheet.write('D1', 'Corresponding Query', bold)
See my code:
mylines = ""
copy = False
row = 1
col = 2
with open(infile, 'rt') as myfile:
for myline in myfile:
if myline.startswith('&'):
# mylines.append(myline.rstrip('\n'))
copy = True
if myline.startswith('@'):
copy = False
if copy:
print(myline, end='')
mylines += myline
worksheet.write(row, col, '\n'.join(myline))
row +=1