2

I just started with Python 3.4.2 and trying to find and replace text in csv file.

In Details, Input.csv file contain below line:

0,0,0,13,.\New_Path-1.1.12\Impl\Appli\Library\Module_RM\Code\src\Exception.cpp

0,0,0,98,.\Old_Path-1.1.12\Impl\Appli\Library\Prof_bus\Code\src\Wrapper.cpp

0,0,0,26,.\New_Path-1.1.12\Impl\Support\Custom\Vital\Code\src\Interface.cpp

0,0,0,114,.\Old_Path-1.1.12\Impl\Support\Custom\Cust\Code\src\Config.cpp

I maintained my strings to be searched in other file named list.csv

Module_RM
Prof_bus
Vital
Cust

Now I need to go through each line of Input.csvand replace the last column with the matched string.

So my end result should be like this:

0,0,0,13,Module_RM  
0,0,0,98,Prof_bus  
0,0,0,26,Vital  
0,0,0,114,Cust  

I read the input files first line as a list. So text which i need to replace came in line[4]. I am reading each module name in the list.csv file and checking if there is any match of text in line[4]. I am not able to make that if condition true. Please let me know if it is not a proper search.

import csv
import re  

with open("D:\\My_Python\\New_Python_Test\\Input.csv") as source, open("D:\\My_Python\\New_Python_Test\\List.csv") as module_names, open("D:\\My_Python\\New_Python_Test\\Final_File.csv","w",newline="") as result:
reader=csv.reader(source)
module=csv.reader(module_names)
writer=csv.writer(result)
#lines=source.readlines()
for line in reader:
for mod in module_names:
    if any([mod in s for s in line]):
        line.replace(reader[4],mod)
        print ("YES")
    writer.writerow("OUT")
    print (mod)
module_names.seek(0)
lines=reader

Please guide me to complete this task.

Thanks for your support!

2
  • try if any([mod in s for s in line.split(',')]): Commented Aug 7, 2015 at 15:40
  • Thanks for your suggestion, actually while reading module list, there is a space at the end of the module name. By using strip(), the extra spacing is removed and now i am able to find sub string in the string. Now moving forward to complete my task. Thanks for your help! Commented Aug 8, 2015 at 8:42

1 Answer 1

2

At-last i succeeded in solving this problem!

The below code works well!

import csv

with open("D:\\My_Python\\New_Python_Test\\Input.csv") as source, open("D:\\My_Python\\New_Python_Test\\List.csv") as module_names, open("D:\\My_Python\\New_Python_Test\\Final_File.csv","w",newline="") as result:
    reader=csv.reader(source)
    module=csv.reader(module_names)
    writer=csv.writer(result)
    flag=False
    for row in reader:
        i=row[4]
        for s in module_names:
            k=s.strip()
            if i.find(k)!=-1 and flag==False:
                row[4]=k
                writer.writerow(row)
                flag=True
        module_names.seek(0)
        flag=False

Thanks for people who tried to solve! If you have any better coding practices please do share!

Good Luck!

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.