How to merge chosen text file that saved in google drive and saved as a single csv file in python?

Viewed 42

My data are stored in different directories on google drive. I want to extract one certain text file from each directory and store them as a single csv file. The csv file called model keeps all the different file names that I need to get. And this is the only part I need to change for searching the files that are qualified.

To be more specific:the model csv file contains the following :['ENS','ENS_hr','ENS_lr','MM5','MM5G','MPAS25','NMM3GFS','NMM3NAM','WRF2GFS','WRF2GFS81','WRF2NAM','WRF2NAM81','WRF3ARPEGE','WRF3GEM','WRF3GFS','WRF3GFSgc01','WRF3NAM','WRF3NAVGEM','WRF4ICON','WRF4NAM','WRF4RDPS']

here is my code:

md = []
model = pd.read_csv(verification_path + 'model_name.csv')
#find file for the correct model
for m in model.iterrows():
  model_file = verification_path + m +'/MAE_monthly_APCP6_60hr_small.txt' 
  new = pd.read_csv(model_file)
  md.append(new)  

But I got the error shows:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-981115533dbc> in <module>
      6 #find file for the correct model
      7 for m in model.iterrows():
----> 8   model_file = verification_path + m +'/MAE_monthly_APCP6_60hr_small.txt'
      9   new = pd.read_csv(model_file)
     10   md.append(new)

TypeError: can only concatenate str (not "tuple") to str

Does anyone have any idea how to solve it? Is there another better way?

Thx!

I tried to convert the tuple by the following code and got the new error:

The code for converting tuple:

import functools
import operator 
def convertTuple(tup):
    str = functools.reduce(operator.add, (tup))
    return str

The updated code:

for m in model.iterrows():
  model_file = verification_path + convertTuple(m) +'/MAE_monthly_APCP6_60hr_small.txt' 
  new = pd.read_csv(model_file)
  md.append(new)

The error message:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/ops/array_ops.py in _na_arithmetic_op(left, right, op, is_cmp)
    165     try:
--> 166         result = func(left, right)
    167     except TypeError:

12 frames
TypeError: unsupported operand type(s) for +: 'int' and 'str'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/pandas/core/roperator.py in radd(left, right)
      7 
      8 def radd(left, right):
----> 9     return right + left
     10 
     11 

TypeError: unsupported operand type(s) for +: 'int' and 'str'
0 Answers
Related