1

I try to do some data processing.
My question as following:

Folder (C://) contains multiple text files.

To read 1st text file -> process(get some data inside) to list1
To read 2nd text file -> process(get some data inside) to list2
.
.
To read Nth text file -> process to listN

Write ([list1],[list2],....,[listN]) into one excel.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 3
    Which tutorials on text processing and excel handling in python did you follow? How far did you get? Please consider splitting this in two questions, one on reading multiple text files, one on writing to an excel file. Please demonstrate how far you got yourself, some own effort is expected before a question on StackOveflow. Please take the [tour] and read [ask]. – Yunnosch Aug 29 '18 at 05:49
  • Search the knowledgebase we build on SO. Read about reading text files (f.e. here: [read text files in folder](https://stackoverflow.com/questions/35672809/how-to-read-a-list-of-txt-files-in-a-folder-in-python) ] as well as how to create excel files (f.e. [write to excel spreadsheet](https://stackoverflow.com/questions/13437727/python-write-to-excel-spreadsheet) . You can also check out pandas which can handle both: f.e. https://xlsxwriter.readthedocs.io/working_with_pandas.html – Patrick Artner Aug 29 '18 at 05:51

1 Answers1

0

To read X Files you need a multidimensional list. It's a list out of lists.

import os
path = "C://folder/"
files = os.listdir(path)

file_list = []
for file in files:
    with open (path + file,"r") as txt:
        file_list.append(txt.read().splitlines())

If .csv is the format you want to write, you would write the file like this:

from csv import writer

with open("test.csv", "w", newline="") as csv:
    write = writer(csv, delimiter=';')
    for file in file_list:
        write.writerow(file)

(This way, every row is a file, and every column is a row of the file)

If you want a .xls/.xlsx file, you could look in the documentation for the module xlsxwriter