Home > database >  python looping through csv files
python looping through csv files

Time:04-17

I'm new to programming. I have a certain amount of csv files. What I want to do is to read the text columns in these files and to translate the columns to Spanish with google translate API, and then save the data frame as a new csv file.

My code goes like this:

!pip install googletrans==4.0.0rc1
from numpy.ma.core import append
import googletrans

import pandas as pd
import numpy as np

from googletrans import Translator
translator = Translator()

df = pd.read_csv("file.csv")
sentences= df['text'].tolist()
result = []
text_es=[]
[result.append(translator.translate(sentence,dest='es')) for sentence in sentences]
for s in result:
  text_es.append(s.text)
df['text_es'] = np.array(text_es)
df.to_csv('es_file.csv', index=False)

Instead of uploading every single file and applying the code, I want to write a code that applies the code to all the files. How can I do this?

CodePudding user response:

Ok so what you're going to want to do is create an Array of paths to all your csv files.

csv_paths = [Path1, Path2, Path3, Path4]

Then you need to loop over this list which is pretty simple simply use a for each loop like this:

for path in csv_paths:

Now you can do almost exactly what you were doing before but inside the loop:

df = pd.read_csv(path)
sentences= df['text'].tolist()
result = []
text_es=[]
[result.append(translator.translate(sentence,dest='es')) for sentence in sentences]
for s in result:
  text_es.append(s.text)
df['text_es'] = np.array(text_es)
df.to_csv('es_file.csv', index=False)

I hope that helps :)

CodePudding user response:

You can list all files in a folder using:

os.listdir('My_Downloads/Music')

And write a loop on this list. See the docs: See this link for more info

  • Related