Replace a list with another list in a file

Asked

Viewed 85 times

0

I’m having trouble doing step 4, I want to create a code takes links in a file and replaces by others in an HTML already pre-formatted with variables @link1@, @link2@..., but it returns the error: AttributeError: 'list' object has no attribute 'replace'

#Passo 1 - Lê arquivo com links
arquivo_open = open(path_links, "r")
arquivo = arquivo_open.readlines()

#Passo 2 - Lê HTML
html_open = open(path_html, 'w')

#Passo 3 - Cria os variáveis que serão substituídas
var = ['@links_' + str(i) + '@' for i in range(1,11)]

#Passo 4 - Procura VAR e substitui por ARQUIVO
for url in enumerate(arquivo):
    html.replace(var[0], arquivo[0])
  • readlines() returns a list, this means that its variable html is a list of the lines of your file, what you want is to replace the links of the HTML file is this?

  • That’s right, I want to take an HTML file and replace the links in it with other links, but they need to match the order of the list

  • lines match: 1 -> 1, 2 -> 2, etc?

  • yes, file line 1 -> @link1@ file line 2 ->@Link2@

No answers

Browser other questions tagged

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