Python Forum
Filter Excel and Convert an Excel File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filter Excel and Convert an Excel File
#1
Hello everyone,

The following is my attempt to filter the last modified xls file on a ftp server and convert that file to a csv on the same server.

from datetime import datetime

import ftplib

import ftputil

import pandas as pd

# Download some files from the login directory.

with ftputil.FTPHost('ftp.123.com', 'username', 'password') as host:

flz= []

name2 = host.curdir

names = host.listdir(host.curdir)

print(host.curdir)

print(name2)

for name in names:

if host.path.splitext(name)[1] == '.xls':

mtime = host.path.getmtime(name)

converttime = datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')

flz.append(name)

print(name,converttime)

for i in range(0, len(flz)):

if i == (len(flz) - 1):

print("This is the last element : " + str(flz[i]))

# Read the file

if host.path.isfile(str(flz[i])):

# Read the file

read_file = pd.read_excel(host.getcwd()(f'{flz[i]}'))

# Reconstruct file name, converting extension from '.xls' to '.csv'

output_csv = f"{host.getcwd()(str(flz[i]).name.replace('.xls', ''))}.csv"

# Create output csv file

read_file.to_csv(output_csv, index=None, header=True)

# Read csv file into DataFrame

df = pd.DataFrame(pd.read_csv(f"{host.curdir(get.cwd() / 'output_csv')}"))

# Output DataFrame

print(df)

print('Done')
When ran I get an error message of:
Traceback (most recent call last):
  File "C:\Users\mrdrj\Desktop\Desk--2021 April 23\Desktop(Update)\ftpdownload.py", line 29, in <module>
    read_file = pd.read_excel(host.getcwd()(f'{flz[i]}'))
TypeError: 'str' object is not callable
How can I solve the filter and convert to csv ftp issue? Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating formulas in Excel - best solution MasterOfDestr 4 561 Mar-11-2024, 01:02 PM
Last Post: MasterOfDestr
  Python openyxl not updating Excel file MrBean12 1 250 Mar-03-2024, 12:16 AM
Last Post: MrBean12
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 546 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Excel from SAP - dealing with formats and VBA MasterOfDestr 7 459 Feb-25-2024, 12:23 PM
Last Post: Pedroski55
  Python best library for Excel reports & review of existing code MasterOfDestr 4 498 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  tables in Excel MaartenRo 3 479 Jan-06-2024, 03:46 PM
Last Post: deanhystad
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 756 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,696 Oct-31-2023, 10:21 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020