Using Line Items created from Code?

  • 21 October 2020
  • 2 replies

Userlevel 1

I’m using a code step to reformat a VTT file to a CSV so I can import it into Google Sheets. I’ve figured out the code and it works great, except I can’t use the information in future steps as line items like I assumed I’d be able to do.


My code:


file = input_data['file'] 
name = input_data['name'] 
segments = file.split('\n\n') #SPLIT ON DOUBLE NEWLINE, IMPORTANT

import re
m = re.compile(r"\<.*?\>")#Strips unwanted tags
o = re.compile(r"\.+\d+")#Strips miliseconds

def clean(content):    
    new_content = m.sub('',content)
    new_content = o.sub('',new_content)
    new_content = new_content.replace('align:start position:0%','')
    new_content = new_content.replace('-->','')
    return new_content

new_segments = [clean(s) for s in segments if len(s)!=0][2:]

def clean_time(time):
    time = time.split(':')
    if time[0]=='00':
        return time[1]+':'+time[2]
    if not time[0]=='00':
        return time[0]+':'+time[1]+':'+time[2]

trimmed_segments = []
for segment in new_segments:
    split_segment = segment.split()
    time_code = split_segment[0] 
    text = ' '.join(segment.split()[2:])
    trimmed_segment = (time_code, text) 

return {'line_items': trimmed_segments}


The below screenshot is what I get in return: 




But when I go to add this information to Google Sheets, it won’t let me choose to add all timecodes to one column and text in another. It works with normal CSV files, but not this one! help!

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +8

@cassiedagmar that’s great news! Would love to hear how you figured this one out- I’m sure it will be helpful to other folks as well!

Userlevel 1

right after i posted this, i solved it! not sure how to delete it! thanks anyway!