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
...
Thanks, I get it but my three sentences turn into one :(
– pitanga
You can use a markup character as a "." and use an if to add a ". n" wherever you have "."
– Carlos H Marques
Thanks, but I can’t use punctuation.
– pitanga