0

I'm currently trying to loop through all my csv files in a given folder (dest_dir), find a certain string in any file that may have it, and replace it with a different string. Whenever the code runs through, Nothing is being updated in any of the csv files. Why isnt this code reading the files and replacing the value with whats in the code?

import pandas as pd
import shutil
import os
import glob
import csv

src_dir = 'H:\\PCoE\\Users\\VanBecelaere_C\\Data Initiative Project\\Tables'
dest_dir = 'H:\\PCoE\\Users\\VanBecelaere_C\\Data Initiative Project\\Updated Tables'
 
files = os.listdir(src_dir)
shutil.copytree(src_dir, dest_dir)
print("Copy to new table complete")

for i in os.listdir(dest_dir):
    files = os.path.join(dest_dir,i)
    split= os.path.splitext(files)
    if split[1]=='.FAC' or split[1]=='.RPT' or split[1]=='.FLX' or split[1]=='.fac':
        os.rename(files,split[0]+'.csv')
    
print("Coverted to CSV")

csv_files = glob.glob(dest_dir + "/*.csv")
for file in csv_files:
    df = pd.read_csv(file, encoding='UTF-8', sep='delimiter', header=1)
    df.replace("NA_NRF", "FA_GUAR")
    df.to_csv(file, index=False)

1 Answers1

0

replace in not in place, use:

for file in csv_files:
    df = pd.read_csv(file, encoding='UTF-8', sep='delimiter', header=1)
    df = df.replace("NA_NRF", "FA_GUAR")
    df.to_csv(file, index=False)

Or even without a variable:

for file in csv_files:
    (pd.read_csv(file, encoding='UTF-8', sep='delimiter', header=1)
       .replace("NA_NRF", "FA_GUAR")
       .to_csv(file, index=False)
     )
mozway
  • 194,879
  • 13
  • 39
  • 75
  • so i added a try and except field to the code you gave me and it is running through the "try" area but still nothing is being replaced in my csv files. I had to add an "encode" section to my pd.read.csv to get past certain unicode errors and I used encoding='ISO-8859–1' but still nothing gets updated at all – chaservan23 Aug 15 '22 at 13:57