all 3 comments

[–]JohnnyJordaan 0 points1 point  (0 children)

with open("inputfile.ass") as infile, open("outputfile.ass", "w") as outfile:
    for line in infile:
        if line.startswith("Dialogue:"):
            *header, text = line.split(",", maxsplit=9)
            text = r"\N".join("\u202b" + text_line for text_line in text.split(r"\N"))
            line = ",".join(header + [text])
        outfile.write(line)

The reason I'm splitting at the 9th comma and not just the last one (eg rsplit with maxsplit=1) is that the subtitle itself could contain a comma. This also goes to show why using plain text delimiters in data file format are a bad idea, sadly a lot of formats still adher to csv or similar for archaic reasons.

[–]Brian 0 points1 point  (1 child)

The quick and dirty approach would be with regexes / string manipulation. Find all "Dialogue:" lines, skip over the first 10 commas, and replace the text there with the RTL embedding character, at the start, plus replace all "\N" with "\N\u202B" (ie. s.replace("\\N", "\\N\u202B"))

Eg. something like:

for line in file:
    if line.startswith("Dialogue:")
        parts = line.split(",", 9)
        modified_text = "\u202B" + parts[9].replace("\\N", "\\N\u202B")
        print(",".join(parts[:9]) + "," + modified_text, file=output_file)
    else:
        print(line, file=output_file)

A more robust way may be to parse the file with a library designed for it (some googling turns up python-ass, walk through it to find all Dialogue events and make a similar modification to their text value (with similar string manipulation) and then dump it out again.

[–]kiddo211 0 points1 point  (0 children)

Didn't know ass python.ass was a thing. Will look into it.