0

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)
1
  • 2
    "Okay it was updated " Okay, now look at the message carefully. Notice how it highlights the line of code, with open(dest_dir + filenames) as f? That's the part where you want to concatenate, right? The dest_dir + filenames part? So - does filenames make sense there? (Hint: what is the type of dest_dir? What is the type of filenames? Do you see how that relates to the error message?) Finally: where the code says for filename in filenames:, does that give a hint as to how to fix the problem? Commented Aug 18, 2022 at 1:17

1 Answer 1

0
with open(dest_dir + filenames) as f:

The error says that you can not add a list to a string. filenames is the list:

filenames = [file + '.csv' for file in files]

So probably you wanted something like:

for filename in filenames:              
        # read header
        with open(dest_dir + filename) as f:

Note that filename is an element of the list filenames

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.