Problems entering values in Excel

Asked 2 years ago, Updated 2 years ago, 34 views

You are creating a program that reads, spaces, and stores text files in the directory in EXCEL.
When executed, only the last data read is output, and the rest of the data is not output or stored.
I thought there was a problem with the for statement, so I tried it, but it didn't work...
Please let me know if there is any solution.

import openpyxl as op
importos.path
import urllib.request
import pyautogui aspg
import subprocess
import time
import glob
from janome.tokenizer import Tokenizer

defmain():

    text_list=glob.glob('./*.txt')
    for text in text_list:
        for i in range (1,5):
            text=text.lstrip('.\\')
            # print(text)
            with open(text)astxt:
                allf = txt.read()

            txt_base=allf.replace('\n', '')
            txt_base=txt_base.replace('\r',')
            txt_base='.join(txt_base.split())
            # print(txt_base)

            t = Tokenizer()
            with open(str(text) + '_wakati' + '.txt', 'w') as fw:
                for token int.tokenize (txt_base, wakati=True):
                    fw.write(str(token))
                    fw.write('')

        with open(str(text) + '_wakati' + '.txt') asow:
            wakati_data=ow.read()
            print(wakati_data)
            wb=op.load_workbook('hoge.xlsx')
            ws = wb.active
            ws['D'+str(i)].value=wakati_data
    wb.save('hoge.xlsx')



if__name__=='__main__':
    main()

python python3

2022-09-29 21:27

2 Answers

I think it's an indentation problem. I think the part that writes to the xlsx file should be inside the for loop that is spinning with the variable i.

for text in text_list:
    for i in range (1,5):
    # Abbreviated #
        with open(str(text) + '_wakati' + '.txt') asow:
            wakati_data=ow.read()
            print(wakati_data)
            wb=op.load_workbook('hoge.xlsx')
            ws = wb.active
            ws['D'+str(i)].value=wakati_data
            wb.save('hoge.xlsx')

As for how to write a questionnaire, it goes through all the loops and then writes to the xlsx file.
(Second for loop and indentation level are aligned)


2022-09-29 21:27

The biggest problem with handling the question is that there are for i in range (1,5): and four confusing iterations.I think it's because i is needed to locate Excel cells, so I think it works by modifying it as follows.There may be other problems, but please correct them appropriately.

 from pathlib import Path# Add only

defmain():
    # Save created *_wakati.txt to directory
    path=Path('./wakati')
    if not path.exists():
        path.mkdir()

    for text in Path('.').glob('*.txt'):
        with open(text)astxt:
            allf = txt.read()

            txt_base=allf.replace('\n', '')
            txt_base=txt_base.replace('\r',')
            txt_base='.join(txt_base.split())

            t = Tokenizer()
            with open(path/(text.stem+'_wakati'+'.txt'), 'w') as fw:
                for token int.tokenize (txt_base, wakati=True):
                    fw.write(str(token))
                    fw.write('')

    # Take out the saved *_wakati.txt and write it to Excel    
    wb=op.Workbook()
    ws = wb.active
    i = 1
    for text in path.glob('*.txt'):
        with open(text) asow:
            wakati_data=ow.read()
            ws['D'+str(i)].value=wakati_data
        i+=1
    wb.save('hoge.xlsx')


2022-09-29 21:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.