×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Loop and process csv files stored in an array list

Loop and process csv files stored in an array list

Loop and process csv files stored in an array list

(OP)
Hi All,
I'm very new in python and trying to experiment on learning to process csv files and its contents. My main problem is that, it runs without an error but it only execute once meaning it process only one csv file containing in an array list. Upon checking the directory where the csv files located, it is visibly contain many csv files.

import glob
import os
import csv

csvArrFile = [] # array list to hold the filenames of csv files
directory = 'C:\\WinScpScript\\' # locati0n of csv files

try:
for csvFilename in os.listdir(directory):
if csvFilename.endswith(".sent"):
csvArrFile.append(csvFilename) ## saved csv filenames contains in array list

os.chdir('C:\\WinScpScript\\')

for csvArray in csvArrFile:
CdrData = open(csvArray, "r") # open csv file on read only mode

## Set up CSV reader and process the header

csvReader = csv.reader(CdrData)
header = next(csvReader)

CallStatIdIndex = header.index("Calling-Station-ID")
NasIPAddrIndex = header.index("NAS-IP-Address")
NasPortIndex = header.index("NAS-Port")
CdrDateTimeIndex = header.index("CDRTimeStamp")

## Loop through the lines in the file and get each corresponding csv info
csvCtr = 0
for row in csvReader:
if csvReader is None:
CdrData.close()

fName = os.path.splitext(csvArray)[0] ##Extract base Filename
if os.path.exists(fName):
os.remove(fName)
Out_CsvFle = (fName)

csvCtr += 1
CdrRawData = []

# process each row contents of the csv file.
CallStatId = row[CallStatIdIndex]
NasIPAddr = row[NasIPAddrIndex]
NasPort = row[NasPortIndex]
CdrDateTime = row[CdrDateTimeIndex]

CdrRawData.append([CallStatId,NasIPAddr,NasPort,CdrDateTime]) #save the data into an array list...

newCdrData = str(CdrRawData).replace('[','').replace(']','').replace("'",'') #reformat the raw data
print("Num of Rows: " + f"{csvCtr:,d}") # display numbers of rows processed
with open(Out_CsvFle, 'a') as Out_Fle :
Out_Fle.write(newCdrData + '\n') # write the data with the same filename as the input file

except(FileNotFoundError,IndexError,IOError) as e:
print(sys.exc_info(e))
finally:
CdrData.close
print("Exit Program.")

Any help would be gladly appreciated.
Thank you in advanced.

RE: Loop and process csv files stored in an array list

Hi

Python code without indentation is pretty useless, so please edit your post and enclose the code between [code] and [/code] TGML tags to preserve formatting.

Feherke.
feherke.github.io

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close