0

I am trying to join every_line in a txt file with a header text. But after successfully joining up the lines. I cannot seem to write the file correctly as it will only write the last joined line into the internallinks.txt. How can I make it to write the whole output of combined into the file?

Any help would be appreciated, thank you very much!

Python code

with open(r"C:\Users\xingj\Desktop\writing.txt") as f:
    internallink = ("www.icom.org.cn")
    for every_line in f:
        combined = (internallink + every_line.strip())
        out_str = "".join(combined)


with open("C:\\Users\\xingj\\internallinks.txt",'w') as b:
    b.write(out_str)

Content of writing.txt

/icom/faculty/viewer/?id=1122
/icom/faculty/viewer/?id=1125
/icom/faculty/viewer/?id=586&
/icom/faculty/viewer/?id=1126
/icom/faculty/viewer/?id=470&

Output of internallinks.txt

www.icom.org.cn/icom/faculty/viewer/?id=470&

Output of command print (combined) before with is closed

PS C:\Users\xingj> & python c:/Users/xingj/testingagain.py
www.icom.org.cn/icom/faculty/viewer/?id=1122
www.icom.org.cn/icom/faculty/viewer/?id=1125
www.icom.org.cn/icom/faculty/viewer/?id=586&
www.icom.org.cn/icom/faculty/viewer/?id=1126
www.icom.org.cn/icom/faculty/viewer/?id=470&
PS C:\Users\xingj>

4 Answers4

1

In the while loop, you are re-assigning the out_str variable to the current value of combined. Instead, for your desired output, you should be appending the new value ,i.e. combined to out_str. Just replace

for every_line in f:
    combined = (internallink + every_line.strip())
    out_str = "".join(combined)

with

for every_line in f:
    combined = (internallink + every_line.strip())
    out_str = out_str + combined

and your code should be fine.

1

You are assigning a new string to the combined variable you have to add the old with assigned combined to assign all of the strings

internallink = "www.icom.org.cn"
combined = ''
for every_line in tt:
    # If you don't want the text on newline you can remove `\n`
    combined = combined + internallink + every_line.strip() + '\n' 

print(combined)

OutPut:-

www.icom.org.cn/icom/faculty/viewer/?id=1122
www.icom.org.cn/icom/faculty/viewer/?id=1125
www.icom.org.cn/icom/faculty/viewer/?id=586
www.icom.org.cn/icom/faculty/viewer/?id=1126
www.icom.org.cn/icom/faculty/viewer/?id=470
Rajas Rasam
  • 175
  • 2
  • 3
  • 9
1

Maybe you'd like a nested approach:

with open(r"C:\Users\xingj\Desktop\writing.txt") as f, open("C:\\Users\\xingj\\internallinks.txt",'w') as b:
    for line in f:
        b.write('www.icom.org.cn'+line)
Red
  • 26,798
  • 7
  • 36
  • 58
  • 2
    I don't consider this a "clean" approach. The output of any given input line doesn't depend on any other input line, so reading the whole file into memory makes no sense. It should read one line, modify it, and write out that line. Repeat until input EOF. – cdlane Jun 02 '20 at 05:16
0

When dealing with input files, I recommend you assume the source is extremely large and code accordingly. For example, dealing with it line by line and not reading the entire file into memory:

with open(r"C:\Users\xingj\Desktop\writing.txt") as input_file:
    with open(r"C:\Users\xingj\internallinks.txt", 'w') as output_file:
        for link in input_file:
            output_file.write('www.icom.org.cn' + link)

You can combine both open() statements into one with statement, but I see no advantage into doing so. If there is one, please comment!

cdlane
  • 40,441
  • 5
  • 32
  • 81