python3 regex pattern to separate items on a line

Question:

I don’t know regex … I have a file with lines like this:

1143 296 'D:\ssTEST\TEST_VIDS IMAGES\2022-11-Tasmania renamed\01\2022_11_08_03_49_03_000000_rot_090_1080x1920__20221108_144850.mp4'
1426 320 'D:\ssTEST\TEST_VIDS IMAGES\2022-11-Tasmania renamed\01\2022_11_08_03_49_22_000000_rot_090_1080x1920__20221108_144908.mp4'
1733 319 'D:\ssTEST\TEST_VIDS IMAGES\2022-11-Tasmania renamed\01\2022_11_08_03_49_45_000000_rot_090_1080x1920__20221108_144932.mp4'

What regex string can separate parts of a line into integer_1, integer2, string_without_quotes ?

ChatGPT has had a few goes and can’t manage it:

#line_parts = re.findall(r"'([^']*)'|S+", line.strip())
#line_parts = re.findall(r"'([^']*)'|d+", line.strip())
#line_parts = re.findall(r"'([^']*)'|bd+b", line.strip())

Thanks

Asked By: hydra3333

||

Answers:

re.findall(r"^(d+)s+(d+)s+'([^']+)'$", line.strip())
Answered By: Amin
import regex as re
with open(filename, 'r') as file:
    lines = file.read()
    for line in lines:
        # findall returns a list containing parts.
        parts = re.findall(r"^([0-9]+)s+([0-9]+)s+'(.*)'$", line. Strip())
        print(parts)

Output:

[('1143', '296', 'D:\ssTEST\TEST_VIDS IMAGES\2022-11-Tasmania renamed\01\2022_11_08_03_49_03_000000_rot_090_1080x1920__20221108_144850.mp4')]
[('1426', '320', 'D:\ssTEST\TEST_VIDS IMAGES\2022-11-Tasmania renamed\01\2022_11_08_03_49_22_000000_rot_090_1080x1920__20221108_144908.mp4')]
[('1733', '319', 'D:\ssTEST\TEST_VIDS IMAGES\2022-11-Tasmania renamed\01\2022_11_08_03_49_45_000000_rot_090_1080x1920__20221108_144932.mp4')

This is a

Answered By: odaiwai
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.