Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

riyawilliams avatar riyawilliams -1 Точки

How to read and index multiple files in python?

Hai,

Can Someone help me in this,

How can we read and index multiple files, from a source folder, in python and append index with the newly added or unread files in the index upon code refresh?

An automation tool is continuously putting files (say xlsx) to the source folder, a python program will then read and plot a graph from all the files present in the folder, to optimize the performance of the code, we are planning to not to read all the files once the code/ application is refreshed but to only append the index with the unread files.

Regards,

Riya.

Python developer

Тагове:
-1
VasilK avatar VasilK 40 Точки

Hi There,

Would you like to open the xlsx files mannualy, one by one, or using a loop?

Would you please share what you have got as code for the moment?

If you need to work with xlsx file you will need to install and import xlsxwriter library.

 

Other useful libraries to manipulate excel files include:

openpyxl, pandas, xlrd, xlwt, xlutils

 

Useful links:

http://www.python-excel.org/

https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html

 

import xlsxwriter
for f in range (1, 11):
    workbook = xlsxwriter.Workbook('source folder' + str(f) + '. File.xlsx')

The code could be structured in away to rename the file once appended with say "r" for read. The newly generated files from the tool will not have it. Therefore when you loop thru the files in the source folder you will consider only these without "r" flag in the name.

import os
import xlsxwriter
import pandas as pd


rootdir = 'D:/Users/Desktop/excel_test/'

# code use for testing purposes to simulate the automated file generation tool
#for f in range (1, 11):
#    workbook = xlsxwriter.Workbook( rootdir + str(f) + '.File.xlsx')
    
    
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        if '_r' not in file:
            
            source = rootdir + file

            # open file and extract data
            # for example: df = pd.read_excel(source, sheet_name='Sheet1')
            # once the excel data is in a pandas dataframe you can use libraries such as seaborn or matplotlib for data visualisation
            
            old_name = os.path.splitext(file)[0]
            new_name = old_name.replace(old_name, old_name+'_r.xlsx')   
            os.rename(source, rootdir + new_name)

         

0
18/09/2018 19:07:57
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.