find and replace with multiple for loops in Python -


I want to update a text file so that all the letters have a space between the letters.

  loops of <2>  for  import sys, fileinput ActiveFileR = open ('text.txt', 'r') while reading the file line Cap 'alphabet' ['A', 'B', 'C', 'D', 'E', 'F', 'G']; Active FileW = Open ('textW.txt', 'w' , 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T For line in ActiveFileR: 'U', 'V', 'W', 'X', 'Y', 'Z'] in: Capelfabet for ReChr1: Capelfbite in ReChr2 for: ActiveFileW.write (line Location (ReChr1 + ReChr2, ReChr1h However, as a result of 676 (26 * 26) lines in the output file for each line in the input file)  

I understand why this happened, but I do not know how to fix it All the places have been added before I go to the next line until you want to keep the same line looping.

Simply modify the line in a local variable, and write it only once: in ActiveXil For these

 : new line = line for ReChr1 in Capelfapet: in CapAlfabet for ReChr2: newline = newline.replace (ReCrFr 1 + RECAIR 2, RECAIR 1 + "+ + RAZR 2 ) ActiveFileWlow (newline)  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -