Python Forum
Replace every 20th space with line feed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace every 20th space with line feed
#1
I have a text file with words only - no punctuation - that I would like to iterate through and replace every 20th space with a new line/line feed.

Here's what I've tried so far.

I tried this code:
# Build array of lines from file, strip newlines

mylines = []                                # Declare an empty list.
with open ('lorem.txt', 'rt') as myfile:    # Open lorem.txt for reading text.
    for myline in myfile:                   # For each line in the file,
        mylines.append(myline.rstrip('\n')) # strip newline and add to list.

# Locate and print all occurences of space " "

substr = " "                  # substring to search for.
for line in mylines:          # string to be searched
  index = 0                   # current index: character being compared
  prev = 0                    # previous index: last character compared
  while index < len(line):    # While index has not exceeded string length,
    index = line.find(substr, index)  # set index to first occurrence of "e"
    if index == -1:           # If nothing was found,
      break                   # exit the while loop.
    print(" " * (index - prev) + " ", end='')  # print spaces from previous
                                               # match, then the substring.
    prev = index + len(substr)       # remember this position for next loop.
    index += len(substr)      # increment the index by the length of substr.
                              # (Repeat until index > line length)
  print('\n' + line);         # Print the original string under the spaces's
I borrowed this from https://www.computerhope.com/issues/ch00...he%20data.

Which doesn't do the trick because it prints the original text under the found text, and in this case looks like a blank line above. Also, this doesn't modify the source file, it just prints to the screen/shell.

Here's an example of what I'd like to do, the words NEWLINE HERE means start a new line. For visual ease, I've numbered each word below to indicate the word's position in the file:
bogus1 bogus2 bogus3 etc. bogus20 NEWLINE HERE

I'm a total noob to python and could really use the help!
Reply
#2
Replacing text in a file can be tricky. Usually simpler is just to write out the correct text (perhaps to a new file). You can always copy the new one over at the end.

The basics of what you're doing seem generally okay, but I don't see anywhere that you're keeping track of how many spaces are found to know when you're at number 20.

I might instead use a regex to find all the spaces, then keep every 20th. Use the index of those spaces to split the string into segments.

# Build array of lines from file, strip newlines
import re
import itertools


mylines = []                                # Declare an empty list.
with open ('lorem.txt', 'rt') as myfile:    # Open lorem.txt for reading text.
    for myline in myfile:                   # For each line in the file,
        mylines.append(myline.rstrip('\n')) # strip newline and add to list.


substr = " "                  # substring to search for.
for line in mylines:          # string to be searched
    segments = []
    last_index = 0
    for match in itertools.islice(re.finditer(rf"{substr}+", line), 19, None, 20):
        segments.append(line[last_index:match.start()])
        last_index = match.end()
    segments.append(line[last_index:])
    print("\n".join(segments))
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce semper placerat purus, nec imperdiet diam consequat non. Mauris ultrices diam nibh, in aliquet mauris consectetur sed. Vestibulum varius nulla sit amet nibh efficitur accumsan. Nam viverra risus quam, a interdum erat scelerisque vel. Proin sapien nibh, mattis vitae arcu ut, pulvinar dapibus est. Nulla lacus sapien, commodo vitae tortor in, molestie maximus neque. Curabitur nec augue a nulla tristique feugiat a ac odio. Vestibulum sed faucibus massa. Maecenas pellentesque leo id erat lacinia malesuada. Donec fermentum ullamcorper quam sit amet accumsan.
Reply
#3
I think I would split and join.
lines = []
words = []
with open ('debug.txt', 'rt') as myfile:
    for line in myfile:
        words += line.split()
        while len(words) >= 20:
            print(' '.join(words[:20]))
            words = words[20:]
    if len(words) > 0:
        print(' '.join(words))
I too think it better to create a new file instead of trying to replace the text in place.
Reply
#4
Both solutions worked, so KUDOS to both of you! I will see if I can figure out how to write to a new file, and if I get stuck, I will follow up. Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Draw bounding boxes on live feed Jerome 0 269 Jan-20-2024, 10:50 PM
Last Post: Jerome
  Command line argument issue space issue mg24 5 1,311 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  How to parse a live feed in Python? Daring_T 2 4,075 Jan-20-2022, 04:17 AM
Last Post: Daring_T
  Feed List items with Integer euras 9 3,916 May-19-2021, 07:45 PM
Last Post: snippsat
  splitting UAV/sat images to smaller pieces in order to feed a CNN hobbyist 0 1,522 Dec-08-2020, 11:48 AM
Last Post: hobbyist
  Rotation Effect on live Webcam Feed Leziiy 0 1,597 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  from global space to local space Skaperen 4 2,301 Sep-08-2020, 04:59 PM
Last Post: Skaperen
  Increasing Line Space In Spyder IDE JoeDainton123 2 2,902 Aug-19-2020, 08:37 PM
Last Post: JoeDainton123
  Read microcontroller eeprom and feed into python to find checksums. mikeak2001 2 2,775 Mar-25-2020, 04:57 PM
Last Post: mikeak2001
  Regex won't replace character with line break Tomf96 2 2,543 Jan-12-2020, 12:14 PM
Last Post: Tomf96

Forum Jump:

User Panel Messages

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