Currently I am trying to write a script that will look inside a bunch of csv files in a folder and find a specific column, see if there is a specific value, and then replace that value with the new one in all of these files. Currently receiving this error: "TypeError: can only concatenate str (not "list") to str". Do I need to add them all to one dataframe first and then try it again? I thought that was what I was doing. Any help would be appreciated. Thanks!
Traceback (most recent call last):
File "Rename_Tool.py", line 55, in <module>
worker(files)
File "Rename_Tool.py", line 38, in worker
with open(dest_dir + filenames) as f:
TypeError: can only concatenate str (not "list") to str
src_dir = 'H:\\PCoE\\Users\\VanBecelaere_C\\Data Initiative Project\\Tables'
dest_dir = 'H:\\PCoE\\Users\\VanBecelaere_C\\Data Initiative Project\\Updated Tables'
file_count = 0
start_time = time.time()
def worker(files):
filenames = [file + '.csv' for file in files]
for filename in filenames:
# read header
with open(dest_dir + filenames) as f:
read_data = f.read()
header = read_data[:read_data.find('!1')]
idx = header.find('\n')
# read data
df1 = pd.read_csv(dest_dir + filename, skiprows=4, encoding='ISO-8859-1', nrows=1) # read column header only - to get the list of columns
dtypes = {}
for col in df1.columns:# make all columns text, to avoid formatting errors
dtypes[col] = 'str'
df1 = pd.read_csv(dest_dir + filename, dtype=dtypes, skiprows=4, encoding='ISO-8859-1', quotechar="'")
# drop footer
df1 = df1.loc[df1['!']=='*']
df1.loc[df['PROD_NAME'].str.contains('NA_NRF'), 'PROD_NAME_NAME'] = 'FA_GUAR'
file_count += 1 # count the fil
worker(files)
with open(dest_dir + filenames) as f? That's the part where you want to concatenate, right? Thedest_dir + filenamespart? So - doesfilenamesmake sense there? (Hint: what is the type ofdest_dir? What is the type offilenames? Do you see how that relates to the error message?) Finally: where the code saysfor filename in filenames:, does that give a hint as to how to fix the problem?