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
  Converting cells in excel to JSON format desmondtay 4 271 May-23-2022, 10:31 AM
Last Post: Larz60+
  Trying to use 2 values from excel in my script but getting error.. cubangt 3 304 May-11-2022, 07:12 AM
Last Post: normanwolf
  Replicate Excel operations with Python Lumberjack 3 412 May-10-2022, 01:44 AM
Last Post: Lumberjack
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 151 May-03-2022, 05:46 PM
Last Post: ahmedbarbary
  Excel to SQLite kgall89 1 199 May-02-2022, 10:05 AM
Last Post: kgall89
  Python, Tkinter, & Excel Extra 2 222 Apr-29-2022, 10:42 PM
Last Post: Extra
  from MSSQL to excel kat35601 1 238 Apr-11-2022, 06:19 PM
Last Post: buran
  please help I cant convert py file to exe idk333333 0 257 Mar-07-2022, 06:16 AM
Last Post: idk333333
  filter every 24 days file (considering file name) RolanRoll 2 420 Mar-04-2022, 09:37 AM
Last Post: ibreeden
  Excel to Python AntonioCruz 2 410 Feb-13-2022, 11:43 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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