Python Forum
printing strings on same line..
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing strings on same line..
#1
import re
with open("svlan.txt") as svlan_file:
     svlan_values = set(svlan_file.read().lower().split())

with open("e320configuration.txt") as conf_file:
    with open("data.txt", "wt") as data_file:
        for line in conf_file:
            if line.lower().strip() not in svlan_values:
                data_file.write(line + "\n")

svlan = open('data.txt','r')
lines = svlan.readlines()
for line in lines:
    if 'gig' in line:
       interface = line
    if 'svlan' in line:
        svlanid = line
        print ("{} {}".format(svlanid,interface))
current output as below
svlan id 95 19
interface gigabitEthernet 2/0/4.9519

svlan id 91 119
interface gigabitEthernet 2/0/5.1011901

svlan id 92 119
interface gigabitEthernet 2/0/5.1011902

svlan id 93 119
interface gigabitEthernet 2/0/5.1011903

expected output
svlan id 95 19 interface gigabitEthernet 2/0/4.9519
svlan id 91 119 interface gigabitEthernet 2/0/5.1011902
Reply
#2
when you don't want a line feed, use:
print('some stuff', end='')
Reply
#3
Hi age of wisdom,

import re
with open("svlan.txt") as svlan_file:
     svlan_values = set(svlan_file.read().lower().split())

with open("e320configuration.txt") as conf_file:
    with open("data.txt", "wt") as data_file:
        for line in conf_file:
            if line.lower().strip() not in svlan_values:
                data_file.write(line + "\n")

svlan = open('data.txt','r')
lines = svlan.readlines()
for line in lines:
    if 'gig' in line:
       interface = line.split(' ')
    if 'svlan' in line:
       vlan = line.split(' ')
       print "%s %s %s" %(interface[2],vlan[3],vlan[4])
tried different combination, but result is same.
Reply
#4
well, it's good once you get advice to a problem you cannot solve on your own, to learn something and implement same solution when you see same problem again
https://python-forum.io/Thread-something...8#pid34638
you need to strip new line '\n' from interface
Don't expect to answer same question for the third time
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 756 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,753 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Printing a specific line from a JSON serpiente 4 3,772 Mar-14-2021, 07:27 PM
Last Post: buran
  printing strings with format leodavinci1990 1 1,581 Aug-21-2020, 02:00 AM
Last Post: micseydel
  Finding multiple strings between the two same strings Slither 1 2,511 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  Printing List in one line bharat_s579 6 4,116 May-26-2019, 08:30 PM
Last Post: perfringo
  For loops, strings and printing words with even characters Drone4four 8 5,102 Oct-05-2018, 09:23 AM
Last Post: volcano63
  Python file parsing, can't catch strings in new line Philia 5 3,956 Apr-29-2018, 01:09 PM
Last Post: snippsat
  lists, strings, and byte strings Skaperen 2 4,213 Mar-02-2018, 02:12 AM
Last Post: Skaperen
  Looking code to convert my strings of line in Dict santosh 1 2,619 Nov-10-2017, 04:49 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020