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 3998356
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:30:55+00:00 2026-05-20T07:30:55+00:00

I have an file with a specific data I would like to pull. The

  • 0

I have an file with a specific data I would like to pull.

The file looks like this:

DS User ID 1  
random garbage  
random garbage  
DS  N user name 1   
random garbage  
DS User ID 2   
random garbage  
random garbage  
DS  N user name 2

So far I have:

import sys  
import re  
f = open(sys.argv[1])

strToSearch = ""

for line in f:
        strToSearch += line

patFinder1 = re.compile('DS\s+\d{4}|DS\s{2}\w\s{2}\w.*|DS\s{2}N', re.MULTILINE)

for i in findPat1:  
    print(i)

My output to screen looks like this:

DS user ID 1  
DS  N user name 1  
DS user ID 2  
DS  N user name 2   

If I write to file using:

outfile = "test.dat"   
FILE = open(outfile,"a")  
FILE.writelines(line)  
FILE.close()  

Everything is pushed to a single line:

DS user ID 1DS  N user name 1DS user ID 2DS  N user name 2 

I can live with the first scenario for the ouput. Ideally though I would like to strip the ‘DS’ and ‘DS N’ from the output file and have it comma separated.

User ID 1,user name 1  
User ID 2, username 2

Any ideas on how to get this accomplished?

  • 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-20T07:30:56+00:00Added an answer on May 20, 2026 at 7:30 am

    It’s difficult to provide a robust solution without understanding the actual input data format, how much flexibility is allowed, and how the parsed data is going to be used.

    From just the sample input/output given above, one can quickly cook up a working sample code:

    out = open("test.dat", "a") # output file
    
    for line in open("input.dat"):
        if line[:3] != "DS ": continue # skip "random garbage"
    
        keys = line.split()[1:] # split, remove "DS"
        if keys[0] != "N": # found ID, print with comma
            out.write(" ".join(keys) + ",")
        else: # found name, print and end line
            out.write(" ".join(keys[1:]) + "\n")
    

    Output file will be:

    User ID 1,user name 1
    User ID 2,user name 2
    

    This code can of course be made much more robust using regex if the format specification is known. For example:

    import re
    pat_id = re.compile(r"DS\s+(User ID\s+\d+)")
    pat_name = re.compile(r"DS\s+N\s+(.+\s+\d+)")
    out = open("test.dat", "a")
    
    for line in open("input.dat"):
        match = pat_id.match(line)
        if match: # found ID, print with comma
            out.write(match.group(1) + ",")
            continue
        match = pat_name.match(line)
        if match: # found name, print and end line
            out.write(match.group(1) + "\n")
    

    Both the examples above assumes that “User ID X” always comes before “N user name X”, hence the respective trailing chars of “,” and “\n”.

    If the order is not specific, one can store the values in a dictionary using the numeric ID as a key then print out the ID/name pair after all input has been parsed.

    If you provide more info, perhaps we can be of more help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data (from a text file), I would like to split
I have a large data set and I would like to read specific columns
I have a ~700 MB binary file (non-text data); what I would like to
I'd like to copy the files that have a specific file extension to a
Hey I have an XML file and I would like to navigate to a
I have to develop an application which parses a log file and sends specific
I have a file in the following format: Data Data Data [Start] Data I
I have a file which is an XML representation of some data that is
I'd like to simulate a file without writing it on disk. I have a
I have about 50 or so Excel workbooks that I need to pull data

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.