Python append line of strings to other line of strings

caesim

I'm new in Pyhton so please don't be hard to me. I'm try to get a file called results.txt which should contain strings from two different files. I have a file called:

to-read.txt:

api.domain.com
domain.com
www.domain.com

and a file called:

to-add.txt:

api
www
account
prod
test
temp

now I'm try to add for every string in to-read.txt the words from to-add.txt but at the beginning. for example for the api.domain.com then I sould get:

api.api.domain.com
www.api.domain.com
account.api.domain.com
prod.api.domain.com
test.api.domain.com
temp.api.domain.com

and so on for the other in to-read.txt. My code looks like this:

f = open("results.txt", "w")
with open("to-read.txt", "r") as r, open("to-add.txt", "r") as a:

    for read in r:
        data = read.strip()
        print(data)

    for word in a:
        words = word.strip()
        print(words)



f.close()

Now I don't know how to append to-add with to-read and save it to results.txt

Shubham Sharma

Use:

with open("to-read.txt") as domains,\
        open("to-add.txt") as prefixes,\
        open("results.txt", "w") as output:
    for d in domains:
        for p in prefixes:
            output.write(f"{p.strip()}.{d.strip()}\n")

        prefixes.seek(0)

The contents of results.txt file should look like:

api.api.domain.com
www.api.domain.com
account.api.domain.com
prod.api.domain.com
test.api.domain.com
temp.api.domain.com
api.domain.com
www.domain.com
account.domain.com
prod.domain.com
test.domain.com
temp.domain.com
api.www.domain.com
www.www.domain.com
account.www.domain.com
prod.www.domain.com
test.www.domain.com
temp.www.domain.com

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to append line with strings at the end of variable?

Python style - line continuation with strings?

Formatting multi line strings in Python

How to copy and append strings in 2-line chunks using perl

Bash: Append text in-line with ^M-polluted strings

How to append strings from a loop to a single output line?

sed append after a line containing multiple unordered strings

File to array of strings (line by line)

get combinations of strings line by line

Python: Log multiple strings to one line

Separate strings by word to a separate line in Python

Python line.replace multiple strings

Python: Find the values for 2 strings in a line of text

Python list for different number of strings per line

Matching multi line strings with regex with python

Python: remove initial number and underscore from strings in a line (but keep other underscores)

Python change strings line by line in all files within a directory

multicolored line with strings linecolllection

Multi-line Strings

New line in JavaScript strings

a line contains strings,separated by ','

Separating strings in a single line

Extract a string inside a line without whitespaces beetween other strings

How to use different line wrapping for strings and other items in Eclipse for Java

Get the 3 Strings behind each other from the line by regex expresion

Is this line of code incorrect for comparing two strings to each other?

Ansible: Looping Multiline Strings into Single Line Strings

Print multi-line strings on same line

Math to calculate line thickness on triangulated line strings