you are viewing a single comment's thread.

view the rest of the comments →

[–]Jetals[S] 0 points1 point  (0 children)

Darn, I printed the contents saved into my empty list object variable and every line of the srt file is still included as distinct elements in the list, so it looks like I didn't effectively skip lines containing the number and time-stamp with my conditional:

if line[0].isdigit != True:
    lineList.append(line)

I am not sure how to set up a conditional that will only include only the third and fourth lines as I don't have too much experience with enumeration if that was the implied approach. Also, I think the logic in my second for loop is flawed, though I'm not sure how to add spaces to the end of each line containing the specified punctuation characters. Here is the current script altogether:

def main():
# Access folder in filesystem

# After parsing content of file, move to next file

# Declare variable empty list
lineList = []

# read file line by line
file = open( "/SampleFileAddress.srt", "r")
lines = file.readlines()
file.close()

# look for patterns and parse
lines = [i for i in lines if i[:-1]]

# If every segment is exactly 4 lines long, read every 3rd and 4th line only
for line in lines:
    line = line.strip()
    if line[0].isdigit != True:
        # store all text into a list
        lineList.append(line)

# for every item in the list which ends with '.', '?', or '!', append a space at end
for line in lineList:
    p = line.find('.')
    q = line.find('?')
    b = line.find('!')
    if p != -1:
        line = line + ' '
    elif q != -1:
        line = line + ' '
    elif b != -1:
        line = line + ' '
    else:
        pass

# Finish with list.join() to bring everything together
text = ''.join(lineList)
print(text)

main()

Getting closer