Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
text file processing
#1
hello,

i have a 'one.txt'-file. 'one.txt' contains 11 sentences - plain text - utf-8 characters.
i want to write a py-script that process 'one.txt' in such way,
that every single sentence in 'one.txt' will begin on a new line and with one empty line between every sentence.

i have managed to:
file = open ('/home/tony/one.txt', r)
lines = file.read()

for cm in lines:
	if cm == '.':
		new =  
which module from the standard library should i use?

thanks in advance
Reply
#2
Can you give an example of the input and output?
Reply
#3
(Sep-24-2018, 07:16 PM)nzcan Wrote:
for cm in lines:
    if cm == '.':
        new =
So if the entire line in the file is a period, that counts as a sentence? No words are needed?

Instead of using an equality check (==), try the in operator.
for line in lines:
    if "." in line:
        # a sentence ends in this line
Reply
#4
i suppose the input should be the initial content of 'one.txt' and the output should be the processed content of the 'one.txt but with the changed content. meaning ... every sentence in 'one.txt' already will begin on new line and there will be an empty line between every sentence.
Reply
#5
I'm not clear on what you are trying to do. Are you making changes just for output purposes, or changing the file itself?

If the latter, you can read it all in and then overwrite the original file with the new version. If something goes wrong, you could lose your file though. Better to write a new file and if that works ok, only then delete the original and rename the new.

Changing a file as you go instead is very tricky if the new files contains data that is longer on a per line basis than the original.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
I expect your existing lines end with '\n' rather than '.'. You possibly want to check the string before that newline character, i.e. line[:-1].

You might like to know that strings have an endswith() method. Returns True if a string ends with the string argument supplied (or any of a tuple of strings).

All things to experiment with in the python interactive shell.

Example:

line = 'Mary had a little lamb!\n'
print(line[:-1].endswith(('.', '?', '!')))
will output:
Output:
True
I am trying to help you, really, even if it doesn't always seem that way
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert text from an image to a text file Evil_Patrick 5 4,396 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,591 Dec-11-2018, 02:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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