Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3323628
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:19:31+00:00 2026-05-17T23:19:31+00:00

I have text line by line which contains many field name and their value

  • 0

I have text line by line which contains many field name and their value seperated by : , if any line does not have any field value then that field would not exist in that line
for example

First line:
A:30 B: 40 TS:1/1/1990 22:22:22
Second line
A:30 TS:1/1/1990 22:22:22
third line
A:30 B: 40

But it is confirmed that at max 3 fields are possible in single line and their name will be A,B,TS.
while writing python script for this, i am facing below issues:
1) I have to extract from each line which are the field exist and what are their values
2) Field value of field TS also have seperator ‘ ‘(SPACE).so unable retrieve full value of TS(1/1/1990 22:22:22)

Output valueshould be extracted like that

First LIne:
A=30
 B=40
 TS=1/1/1990 22:22:22

Second Line:
A=30

 TS=1/1/1990 22:22:22

Third Line
A=30
 B=40

Please help me in solving this issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:19:32+00:00Added an answer on May 17, 2026 at 11:19 pm
    import re
    a = ["A:30 B: 40 TS:1/1/1990 22:22:22", "A:30 TS:1/1/1990 22:22:22", "A:30 B: 40"]
    regex = re.compile(r"^\s*(?:(A)\s*:\s*(\d+))?\s*(?:(B)\s*:\s*(\d+))?\s*(?:(TS)\s*:\s*(.*))?$")
    for item in a:
        matches = regex.search(item).groups()
        print {k:v for k,v in zip(matches[::2], matches[1::2]) if k}
    

    will output

    {'A': '30', 'B': '40', 'TS': '1/1/1990 22:22:22'}
    {'A': '30', 'TS': '1/1/1990 22:22:22'}
    {'A': '30', 'B': '40'}
    

    Explanation of the regex:

    ^\s*      # match start of string, optional whitespace
    (?:       # match the following (optionally, see below)
     (A)      # identifier A --> backreference 1
     \s*:\s*  # optional whitespace, :, optional whitespace
     (\d+)    # any number --> backreference 2
    )?        # end of optional group
    \s*       # optional whitespace
    (?:(B)\s*:\s*(\d+))?\s*  # same with identifier B and number --> backrefs 3 and 4
    (?:(TS)\s*:\s*(.*))?     # same with id. TS and anything that follows --> 5 and 6
    $         # end of string
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file which contains a time stamp on each line. My
If I have a file name myownfile.txt which contains 3 lines of text. foo
I have a Text File Of Around 36gb which contains words per line, i
I have a text file which contains many (over 5000) links. Like this: http://www.website.com/rgfdefvrggh
I have a text file in which any line that starts with a single
I have a text file which contains lines, such that every line contains no
I have a text file which contains a line as a,b,a,b,a,b and I want
I have a text file which has a particular line something like sometext sometext
I have a label which labels the line numbers based on the text on
I have a text file that contains unix style line endings: a 0x0A at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.