Home > Enterprise >  Making two columns by skipping a string in Python?
Making two columns by skipping a string in Python?

Time:11-14

For example, let's say I have a file called test.txt, the content is as follows, thousands of strings under each other.

I will come one day.
What did you do.
I will go home.
OK, I'll come too.
I'm waiting then.
I will come.

What I want is that the first two lines are written side by side. When they are written side by side, put the special character I want between them. So like this

I will come one day. ■What did you do?
I'll go home. ■Okay, I'll come too.
I'm waiting then. ■I'll come.

I need a code so that I can print them like this. I've been searching for exactly four hours, but I finally gave up and I'm going to sleep. Thanks everyone in advance.

CodePudding user response:

If your input line count is even, or it's fine if last one got lost with odd number of lines, try this:

>>> with open("src.txt") as src, open("output.txt", "wt") as output:
...     while (line1 := src.readline()) and (line2 := src.readline()):
...         output.write(f"{line1.strip()} # {line2.strip()}\n")

src.txt:

I will comeone day.
What did you do.
I will go home.
OK, I'll come too.
I'm waiting then.
I will come.

output.txt:

I will comeone day. # What did you do.
I will go home. # OK, I'll come too.
I'm waiting then. # I will come.


Or if you need it to work with odd number too, try this:

>>> with open("src.txt") as src, open("output.txt", "wt") as output:
...     while (line1 := src.readline()) and (line2 := src.readline()):
...         output.write(f"{line1.strip()} # {line2.strip()}\n")
...     if line1 and not line2:
...         output.write(line1)

src.txt:

I will comeone day.
What did you do.
I will go home.
OK, I'll come too.
I'm waiting then.
I will come.
Meow

output.txt:

I will comeone day. # What did you do.
I will go home. # OK, I'll come too.
I'm waiting then. # I will come.
Meow

This second one will throw error when there's only one line in text file, but since you said thousands, should be fine.

CodePudding user response:

Prints all lines in the file with the special character in between

special_char = "#"

with open("text.txt", "r") as txt:
    lines = txt.readlines() # read all lines in the file

lines = [line.strip("\n") for line in lines] # remove newline charactor from its end
n = len(lines) #find the number of lines

if n%2 == 0:  # if number of lines is even
    for i in range(0,n,2):  # go from 0 to n with 2 steps at a time, i.e, i = i 2
        print(f"{lines[i]} {special_char} {lines[i 1]}")

else:  # if number of lines is odd
    for i in range(0,n-1,2):
        print(f"{lines[i]} {special_char} {lines[i 1]}")
    print(lines[-1])  # print the last line in the list

Output:

Even number of lines

I will come one day. # What did you do.
I will go home. # OK, I'll come too.
I'm waiting then. # I will come.

Odd number of lines

I will come one day. # What did you do.
I will go home. # OK, I'll come too.
I'm waiting then. # I will come.
See you then
  • Related