1
votes

I am new to Stack Overflow site and Python too.

I have a text file which contains transaction of a company with pipe delimiter

There are certain transactions which comes on the second line due to line character.These transactions typically have one more line character in the second line too.

i want to bring the second line and concatenate it with the first line using python.

A sample file given below for my friends to facilitate understanding my issue (Line Numbers are just for our reference purposes):

Line1: A|B|C|D Line2: A|B|C| Line3: D Line4: Line5: A|B Line6: C|D Line7: Line8: A|B|C|D

Thanks alot for reading this and for providing the solution.

1
Welcome to SO! Did you try anything yet? Feed us with some code!nicovank
Please consider accepting the answer that worked best for you by clicking ✓ on the left (see How to accept SO answers) and upvoting (see How to upvote on Stack Overflow?) all answers that turned out helpful to you.Wiktor Stribiżew

1 Answers

1
votes
while line in fin:
    if line.strip().endswith('|'):
        fou.write(line.strip())
    else:
        fou.write(line)

This simple code will do. Assuming fin, fou are input and output files you've opened.

If a line ends with '|', new line will be written after strip() method which will erase \n or any spacing.