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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:56:37+00:00 2026-06-01T15:56:37+00:00

I’m a python novice and am struggling with some concepts here – any help

  • 0

I’m a python novice and am struggling with some concepts here – any help is appreciated.

I’ve got a custom system tool that queries a database, and returns several lines as results to be read — one on each line. The following python script accepts the site FQDN from raw_input and runs $path on that fqdn.

#!/usr/bin/python

import subprocess
import getpass

#get the site name.
site = raw_input("What is the name of the site?: ").strip()

#run path.
cmd = 'path '+ site;
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE);
path_output = p.stdout.read().strip().split('\n')

print path_output

Which returns results like this this:

['    fqdn          = www.hcasc.info', '    account_id    = 525925', '    parent_id     = 525925', '    nfs           = /mnt/stor7-wc2-dfw1/525925/www.hcasc.info', '  server_type   = PHP5', '    ssl           = False', '    host_ip       = 98.129.229.186', '    cgi_hosting   = False', '    test_link_ip  = 98.129.229.186', '    ipv6_ip       = 2001:4800:7b02:100::1600:0']

How can I get that extra whitespace out from the “nfs = etc”, or just take the third column (aka awk ‘{print $3}’) and/or assign each piece of these results from bash to separate variables for further manipulation?

Just having some trouble mounting this learning curve, your help is sincerely appreciated.

  • 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-06-01T15:56:39+00:00Added an answer on June 1, 2026 at 3:56 pm

    The third column would be line.split()[2]; if you want to throw out the first two words and take the rest, it’s line.split(None, 2)[-1]. (split with no arguments, or a None as the first argument, splits on any whitespace.)

    >>> '    fqdn          = www.hcasc.info'.split()
    ['fqdn', '=', 'www.hcasc.info']
    
    >>> for var, equals, rest in (l.split(None, 2) for l in path_output):
        assert equals == '='
        print var, 'is', rest
    
    fqdn is www.hcasc.info
    account_id is 525925
    parent_id is 525925
    nfs is /mnt/stor7-wc2-dfw1/525925/www.hcasc.info
    server_type is PHP5
    ssl is False
    host_ip is 98.129.229.186
    cgi_hosting is False
    test_link_ip is 98.129.229.186
    ipv6_ip is 2001:4800:7b02:100::1600:0
    

    Explanation:
    (l.split(None, 2) for l in path_output) is a generator expression, which runs l.split(None, 2) for each value of path_output (calling it l). It’s like a list comprehension, which is the same thing but with [] around it instead of (), but it only runs the l.split call as the for loop passes over it and then forgets the previous values, whereas the list comprehension would construct one big list with all the results of l.split at each step first, and then loop over that list normally. This way is like doing

    for line in path_output:
        var, equals, rest = line.split(None, 2)
        ...
    

    but a little shorter. 🙂


    If you want to put this into a dictionary, as DSM suggests, you could do this in this way (just for context) as

    d = dict((var, rest) for var, equals, rest in (l.split(None, 2) for l in path_output))
    

    or, in Python 2.7 / 3, the much nicer

    d = { var: rest for var, equals, rest in (l.split(None, 2) for l in path_output) }
    

    Of course, you could make this a little more readable on two lines:

    output_vals = (l.split(None, 2) for l in path_output)
    d = dict((var, rest) for var, equals, rest in output_vals)
    

    Whether you want a dictionary or just a loop depends on what processing you’re going to be doing with it, but the dictionary is probably a better approach for most things.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6

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.