Replace words between two files

Asked

Viewed 206 times

2

I’m trying to do some overwriting between two files: if a word from file 1 is in the second column of file 2, replace that word from file 1 with the word from the first column of file 2

File 1:

bought big expected big rug surprise package small size consequence wanted to check content consistent order driver delivery not allowed yet recommends order

personal transport shop complete passage work everywhere shops without person occupies genent circulation overview take example shop forum

buy time product damaged address table time wanted order copy buy product damaged send strength

File 2:

buy,

place,

shop,

Script:

import csv


with open ("arquivo1.txt", "r") as f, open("arquivo2.csv", "r") as f1:
    text = f.read().split('\n')
    text_csv = csv.reader(f1, delimiter = ',')

    for item in text: #percorro a lista de strings
        for novo_item in item.split(): #tento separar cada frase em palavras sem perder a info de que é uma frase

            for elements in text_csv: #percorro a lista do arquivo 2
                lexema = elements[1] # colunas
                lema = elements[0]

                if novo_item == lexema: #se um elemento do meu arquivo 1 esta na segunda coluna do arquivo 2
                    novo_item = novo_item.replace(novo_item, lema) #substituir essa palavra pela primeira coluna do arquivo 2

                print (novo_item)

expected output:

buy big expected big package consequence surprise small size wanted to check content consistent order driver delivery not allowed yet recommends order

Personal transport complete passage work shop in all the place shop without person occupies genent circulation overview take example shop forum

buy time product damaged address table time wanted order copy buy product damaged send strength

My output:

bought

bought

bought

bought

...

buy

buy

buy

buy

...

2 answers

3


Another way that would keep the " n" in the text is like the code below, which is even simpler than yours.

import csv
with open ("arquivo1.txt", "r") as f, open("arquivo2.csv", "r") as f1:
    text = f.read()
    text_csv = csv.reader(f1, delimiter = ',')
    for elements in text_csv:
        novo_text= text.replace(elements[1], elements[0])
        text = novo_text
print novo_text

1

I think it would be something like

# coding=utf-8

import csv
aa = ""

with open ("arquivo1.txt", "r") as f, open("arquivo2.csv", "r") as f1:
    text = f.read().split('\n')
    text_csv = csv.reader(f1, delimiter = ',')
    for item in text: #percorro a lista de strings
        for novo_item in item.split(): #tento separar cada frase em palavras sem perder a info de que é uma frase

            for elements in text_csv: #percorro a lista do arquivo 2
                lexema = elements[1] # colunas
                lema = elements[0]

                if novo_item == lexema: #se um elemento do meu arquivo 1 esta na segunda coluna do arquivo 2
                    novo_item = novo_item.replace(novo_item, lema) #substituir essa palavra pela primeira coluna do arquivo 2
                # print (novo_item)
            aa+=novo_item+' '

print aa
  • Thanks, I get it but my three sentences turn into one :(

  • 1

    You can use a markup character as a "." and use an if to add a ". n" wherever you have "."

  • Thanks, but I can’t use punctuation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.