Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to extract the most recent timestamp of data #583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jiuyuan-light opened this issue Nov 13, 2024 · 0 comments
Open

How to extract the most recent timestamp of data #583

jiuyuan-light opened this issue Nov 13, 2024 · 0 comments

Comments

@jiuyuan-light
Copy link

jiuyuan-light commented Nov 13, 2024

Hello everyone, I have the following data (in the code) and I would like to extract the closest timestamp to the data, such as "Tue Nov 12 02:30:51 2000". However, the result I obtained is not what I wanted. What should I do, thank you.

`['Tue Nov 12 01:30:51 2000', 'cmd_prefix a\n cmd output1\n']

  • DATA: ['cmd_prefix a\n cmd output1\n']
  • TIMESTAMP: ['Tue Nov 12 01:30:51 2000']`

data = """
[Tue Nov 12 01:30:51 2000]
abcd
[Tue Nov 12 02:30:51 2000]
1234
cmd_prefix a
    cmd output1
cmd_prefix b
[Tue Nov 12 03:30:51 2000]
abcd
[Tue Nov 12 04:30:51 2000]
1234
cmd_prefix a
    cmd output2
cmd_prefix b
"""

timestamp = (
    LineStart()
    + Suppress(Optional(Literal("[")))
    + Regex(r"\w{3} \w{3}\s+\d{1,2} \d{2}:\d{2}:\d{2} \d{4}")
    + Suppress(Optional(Literal("]")))
)
cmd_prefix_cmd = LineStart() + Literal("cmd_prefix")
cmd_prefix_m_cmd = Combine(cmd_prefix_cmd + Literal(" a"))
cmd_prefix_z_cmd = Combine(cmd_prefix_cmd + Literal(" b"))
cmd_prefix_parse = Suppress(SkipTo(cmd_prefix_m_cmd)) + SkipTo(cmd_prefix_z_cmd)

timestamp_with_data = Group(
    OneOrMore(timestamp)("TIMESTAMP")
    + FollowedBy(cmd_prefix_parse)
    + cmd_prefix_parse("DATA")
)

parser = OneOrMore(timestamp_with_data)

results = parser.parseString(data)

for result in results:
    print(result.dump())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant