Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop and process csv files stored in an array list

Status
Not open for further replies.

viclap

Programmer
Oct 12, 2000
39
0
0
PH
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.

 
Hi

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


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top