Python Forum
Compare and replace - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Compare and replace (/thread-16967.html)



Compare and replace - veltor88 - Mar-22-2019

Bonjour,

I have 2 files .txt i am trying to find a script for compare et replace between 2 files. I heard about python but i dont know how

Example

txt1:

Quote:2000 Yay+0
2001 Yay+1
2002 Yay+2
2003 Yay+3
2004 Yay+4
2005 Yay+5
2006 Yay+6
2007 Yay+7
2008 Yay+8
2009 Yay+9

txt1:

Quote:2000 Arc court+0
2001 Arc court+1
2002 Arc court+2
2003 Arc court+3
2004 Arc court+4
2005 Arc court+5
2006 Arc court+6
2007 Arc court+7
2008 Arc court+8
2009 Arc court+9

But i have more 7000 lines to translate

Thanks you in advance


RE: Compare and replace - gontajones - Mar-22-2019

With Python you can handle files content very easily.
But I don't think that someone here will do it for you.
What have you done so far? Post some code lines here to attract more people to help you.


RE: Compare and replace - veltor88 - Mar-22-2019

Thanks for you answer
[Image: Sans-titre.png]

This is what i want to do, replace line of text 2 to text 1


RE: Compare and replace - gontajones - Mar-22-2019

You can start playing around with files like this Comparing two Files..
Then post your code here.


RE: Compare and replace - snippsat - Mar-22-2019

(Mar-22-2019, 12:12 PM)gontajones Wrote: I heard about python but i dont know how
Python is one of there more easy programming languages to start with,but it still take some effort to learn the basic Wink
If you just want to solve a task and not learn Python,it's more a post for jobs section of forum.

To give a example that almost should solve it,try writing to file yourself.
1.txt:
Output:
11111 test bar+8 12220 foo bar+0 12221 foo bar+1 12222 foo bar+2 99999 car blue+9
with open('1.txt') as f:
    for line in f:
        if 'foo bar' in line:
            line = line.replace('foo bar', 'red car')
            print(line.strip())
        else:
             print(line.strip())
Output:
11111 test bar+8 12220 red car+0 12221 red car+1 12222 red car+2 99999 car blue+9



RE: Compare and replace - gontajones - Mar-22-2019

(Mar-22-2019, 01:28 PM)snippsat Wrote:
(Mar-22-2019, 12:12 PM)gontajones Wrote: I heard about python but i dont know how

I do know how! Big Grin