user9624111
user9624111

Reputation:

Read different lines in different files and write in one sentence

in f1 i have a file with these content

1000000000001
1000000000010
1000000000011 ...

in f2 same content but with different numbers i want the result in f3 to be like this formated:

13'b1000000000001: y<= 6'b1000000000011;
13'b1000000000111: y<= 6'b1000000000010;

Code:

    f1 = open("/home//Downloads/Telegram Desktop/x_z.txt", "r")
f2 = open("/home//Downloads/Telegram Desktop/y_z.txt", "r")
f3 = open("/home//Downloads/Telegram Desktop/final.txt", "w")

lines1 = f1.readlines()
lines2 = f2.readlines()

for line1, line2 in zip(lines1, lines2):
    f3.write(f"13'b{line1}: y<= 6'b{line2};")

result:

13'b1000000000001
: y<= 6'b100111

Thank you for yuor time

Upvotes: 0

Views: 36

Answers (1)

zvone
zvone

Reputation: 19352

readlines() keeps the newlines at the end of each line. This is what is causing you troubles.

One thing you could do is the explicit removal:

line1 = line1.strip('\n')
line2 = line2.strip('\n')

Then again, you can simplify the whole thing like this:

with open("/home//Downloads/Telegram Desktop/x_z.txt", "r") as f1,
    open("/home//Downloads/Telegram Desktop/y_z.txt", "r") as f2,
    open("/home//Downloads/Telegram Desktop/final.txt", "w") as f3:

    for line1, line2 in zip(f1, f2):
        f3.write("13'b{}: y<= 6'b{};\n".format(
            line1.strip('\n'), line1.strip('\n')))

Now you will even have the files closed for you at the end.

Upvotes: 1

Related Questions