Python Forum
Adding new line in a one line txt file.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding new line in a one line txt file.
#1
I converted a pdf to txt. It returned the file where the whole text is written in one line. In order to work with the file i need to add 2 new lines before a number and one after it. (btw. i am using python 3.6)

F.e.:
Input:
Here is some text. It is written in one lines. 12.13. Here is some more text. 2.12.14. Here is even more text.

Output(i wish to have):
Here is omse text. It is written in one lines.

12.13.
Here is some more text.

2.12.14.
Here is even more text.

This is my code. The code runs , but unfortunatly reautrns an empty page. I would be glad about some editing advice.


in_file2 = 'work1-T1.txt'
out_file2 = 'work2-T1.txt'


start_rx = re.compile('|'.join(
    ['\d\d\.\d\d\.', '^\d\.\d\d\.\d\d']))


with open(in_file2,'r', encoding='utf-8') as fin2, open(out_file2, 'w', encoding='utf-8') as fout2:
    text_list = fin2.read().split()

    for line in in_file2:
        start = True
        if re.match(start_rx, line):
            line = line.replace(start_rx, '\n\n' + start_rx + '\n')

        if line == True:
            fout2.write(line)
Reply
#2
It is for line in fin2:
Reply
#3
Thanks for your reply, i eddited it to fin2, but it still returns an empty file :(
Reply
#4
The if line == True is always false because line is a string. What's its purpose?
Reply
#5
well i used it from a different code and adjusted it because. but you are right. Here it makes no sense, i thought it would always return true so it would be no problem. I adjusted it to string .... but still it returns an empty file.

Here the code:
with open(in_file2,'r', encoding='utf-8') as fin2, open(out_file2, 'w', encoding='utf-8') as fout2:
    text_list = fin2.read().split()

    for string in fin2:
        if re.match(start_rx, string):
            string = str.replace(start_rx, '\n\n' + start_rx + '\n')

        fout2.write(string)
Reply
#6
Now the problem is that after fin2.read(), the file pointer is at the end of the file. You can go back to the beginning of the file it by calling seek
fin2.seek(0)
before the for string in fin2
Reply
#7
thank you, that fixed the output problem. But now it returns the exact same as the input. I figure something is wrong with my regex then ?
Reply
#8
Use re.sub() for replacing regex matches in a string
def foo(match):
    return '\n\n' + match.group(0) + '\n'

...
line = start_rx.sub(foo, line)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Βad Input on line 12 Azdaghost 5 1,389 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 348 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Insert command line in script lif 4 1,090 Mar-24-2025, 10:30 PM
Last Post: lif
  Entry field random pull from list, each return own line Bear1981 6 890 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  How to revert back to a previous line from user input Sharkenn64u 2 1,110 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 2,529 Sep-09-2024, 08:03 AM
Last Post: pinkang
  Simplest way to run external command line app with parameters? Winfried 2 1,400 Aug-19-2024, 03:11 PM
Last Post: snippsat
  Printing the code line number arbiel 6 1,763 Jun-30-2024, 08:01 AM
Last Post: arbiel
  How to add multi-line comment section? Winfried 2 1,431 Jun-04-2024, 07:24 AM
Last Post: Gribouillis
Information Is it possible to multi line a Basic Function Construct line statement? If so how? BrandonKastning 7 2,136 May-23-2024, 03:02 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020