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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:48:09+00:00 2026-06-03T20:48:09+00:00

I’m using Python 3. I’ve written two programs. One loops through a csv file

  • 0

I’m using Python 3. I’ve written two programs. One loops through a csv file to obtain IP addresses for Cisco switches, logs in, runs a command, and outputs to results for each to an individual text file. So I end up with a number of text files…one for each switch. The second program uses xlwt to write the information in each text file to its own sheet in Excel.

The main idea is that I need to develop a report showing ports in and out of service. Once I get these imported into Excel I can write some formulas to extract the data I need. But as it stands now when I import this into Excel I have to manually remove some of the cells because everything doesn’t line up and that’s because of the spaces in between some of the words in the name column (I’m importing into Excel as space delimited). I’ve attempted to do some things with string and list methods (split, join, slicing, etc) but I’m not able to get exactly what I want. And the Name column is not standardized in any kind of consistent convention. I do notice that although the name may actually be very long, it gets truncated to a certain number of characters.

Ideally, removing the first 4 lines (there is blank line at the very top) and the last line first, then for anythting between Port and Status, remove it all (remove the column altogether including the header).

This is how the files look after getting data from the switch.

sw1#term length 0
sw1#show interfaces status

Port      Name               Status       Vlan       Duplex  Speed Type
Gi0/1     Trunk to switch (a connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/2     Network augment pe connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/3                        connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/4                        connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/5                        notconnect   1            auto   auto Not Present
Gi0/6                        notconnect   1            auto   auto Not Present
Gi0/7                        notconnect   1            auto   auto Not Present
Gi0/8                        notconnect   1            auto   auto Not Present
Gi0/9                        notconnect   1            auto   auto Not Present
Gi0/10                       connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/11                       notconnect   1            auto   auto Not Present
Gi0/12                       connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/13                       disabled     1            auto   auto Not Present
Gi0/14                       disabled     1            auto   auto Not Present
Gi0/15                       disabled     1            auto   auto Not Present
Gi0/16                       disabled     1            auto   auto Not Present
sw1#logout

End result I’d like below. This should allow the row/column structure to remain intact when importing into Excel. Note that all of the column info is separated by spaces. I’ve found that importing as fixed width or delimited by space with treat consecutive spaces as one checked seems to work pretty well.

Port      Status       Vlan       Duplex  Speed Type
Gi0/1     connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/2     connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/3     connected    1          a-full a-1000 10/100/1000BaseTX
Gi0/4     connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/5     notconnect   1            auto   auto Not Present
Gi0/6     notconnect   1            auto   auto Not Present
Gi0/7     notconnect   1            auto   auto Not Present
Gi0/8     notconnect   1            auto   auto Not Present
Gi0/9     notconnect   1            auto   auto Not Present
Gi0/10    connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/11    notconnect   1            auto   auto Not Present
Gi0/12    connected    1          a-full  a-100 10/100/1000BaseTX
Gi0/13    disabled     1            auto   auto Not Present
Gi0/14    disabled     1            auto   auto Not Present
Gi0/15    disabled     1            auto   auto Not Present
Gi0/16    disabled     1            auto   auto Not Present

Any pointers would be appreciated. I’m thinking regular expressions may be in order but I need a bit assistance with how to construct that. I hope this isn’t too ambiguous.

REMOVED A PREVIOUS UPDATE AND MOVED IT TO A NEW THREAD

  • 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-03T20:48:11+00:00Added an answer on June 3, 2026 at 8:48 pm
    with open('file') as f:
        lines = f.readlines()
        lines = lines[-1:] + lines[2:-1]
        for line in lines:
            print line[0:11] + line[35:-1]
    

    i think that will do roughly what you want; you may need to play with the numbers a little as i haven’t run it myself. all it uses is list (or string) indexing:

    • list[x:] is all entries from x onwards
    • list[x:y] is all entries from x to y
    • list[-x] is the xth line from the end

    the lines[-1:] + lines[2:-1] puts the last line first, and throws away the first two; line[0:11] + line[35:-1] excludes the part you don’t want and the final newline.

    update if you want to write into a new file, instead of stdout:

    with open('infile') as in:
        with open('outfile', 'w') as out:
            lines = in.readlines()
            ...
                print(line[0:6] + line[28:-1], file=out)
    

    in fact, since readlines reads everything at once, you could do:

    with open('infile') as in:
        lines = in.readlines()
    with open('outfile', 'w') as out:
        for line in lines:
            ....
            print(line[0:6] + line[28:-1], file=out)
    

    as there’s no need for the input file to be open (it’s closed when the with finishes).

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
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
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want use html5's new tag to play a wav file (currently only supported

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.