0

I have this code that replaces a string for another string and I'm wondering if there's an easier way to do this. Instead of doing the .replace for each one, I want to just read a column and replace it with the column next to it instead of writing out .replace.

text = open('thecode.csv','r') text = ''.join([i for i in text]) \ .replace('Salam', 'Hello') \ .replace('somebody', 'anybody') \

How can I go about this quicker if I have two columns that look like this:

Column1         Column2
Salam            Hello 
somebody         anybody

2 Answers 2

1

This would be easier in Pandas.

import pandas as pd
df = pd.read_csv('thecode.csv') 
df['Column1'] = df['Column2']
df.to_csv('fixed_output.csv', index=False)
Sign up to request clarification or add additional context in comments.

Comments

0

may I suggest using Pandas? read your file using pd.read_csv simply create a Series from your new column and replace it with the column in the correction position where you want. don't foreget to use to_csv at the end to write the result.

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.