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>
3
  • 5
    You are reassigning the string in each iteration, not appending to it. out_str = "".join(combined) Commented Jun 1, 2020 at 16:22
  • I see. Thank for replying! Sorry I'm just a newbie coder. How can I fix this? Commented Jun 1, 2020 at 16:24
  • How do i append one string to another in python Commented Jun 1, 2020 at 16:25

4 Answers 4

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

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

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.