suppress newlines in my script

S

sloan949

I am importing lines from an external csv file and when I iterate through the lines and increment, new lines are introduced.
How would I cut out the newlines. I have attempted several pythonic strip() and rstrip() how can i implent this?


import sys, os

f=open('europe_csv')
lines=f.readlines()

BU = 'Company,,,,,,,,,,,,,,'
PPP = 'Pre-Prod,,,,,,,,Prod,,,,,,'
C1 = ',,,,,,,,,,,,,,'
Title = 'Site,Environment,'
NET1 = lines[4]
GW1 = lines[5]
M1 = lines[6]
PS1 = lines[7]
PE1 = lines[8]
C2 = ',,,,,,,,,,,,,,'
NET2 = lines[10]
GW2 = lines[11]
M2 = lines[12]
PS2 = lines[13]
PE2 = lines[14]



for count in range(64, 127):
print NET1.format(count)
print GW1.format(count)
print M1
print PS1.format(count)
print PE1.format(count)
print C2
print NET2.format(count)
print GW2.format(count)
print M2
print PS2.format(count)
print PE2.format(count)
 
D

Dennis Lee Bieber

I am importing lines from an external csv file and when I iterate through the lines and increment, new lines are introduced.
How would I cut out the newlines. I have attempted several pythonic strip() and rstrip() how can i implent this?


import sys, os

f=open('europe_csv')
lines=f.readlines()
You state it is a comma separated file... Python has, for years, a
module just for handling CSV (or tab separated, or even others if you
define the "dialect").
BU = 'Company,,,,,,,,,,,,,,'
PPP = 'Pre-Prod,,,,,,,,Prod,,,,,,'
C1 = ',,,,,,,,,,,,,,'
Title = 'Site,Environment,'
NET1 = lines[4]

What is "lines[4]" supposed to look like?

for count in range(64, 127):
print NET1.format(count)

And what do you expect this to produce? NET1 is being used as a
template into which you are placing (if it has a placeholder) integers
in the range 64..126


An example of the input file, and an example of what you expect to
produce with it, would be useful.
 
S

sloan949

I am importing lines from an external csv file and when I iterate through the lines and increment, new lines are introduced.

How would I cut out the newlines. I have attempted several pythonic strip() and rstrip() how can i implent this?





import sys, os



f=open('europe_csv')

lines=f.readlines()



BU = 'Company,,,,,,,,,,,,,,'

PPP = 'Pre-Prod,,,,,,,,Prod,,,,,,'

C1 = ',,,,,,,,,,,,,,'

Title = 'Site,Environment,'

NET1 = lines[4]

GW1 = lines[5]

M1 = lines[6]

PS1 = lines[7]

PE1 = lines[8]

C2 = ',,,,,,,,,,,,,,'

NET2 = lines[10]

GW2 = lines[11]

M2 = lines[12]

PS2 = lines[13]

PE2 = lines[14]







for count in range(64, 127):

print NET1.format(count)

print GW1.format(count)

print M1

print PS1.format(count)

print PE1.format(count)

print C2

print NET2.format(count)

print GW2.format(count)

print M2

print PS2.format(count)

print PE2.format(count)

Thanks for the tip about the CSV module. I did not know about that.

Here are two lines from the CSV file:

,,172.20.{0}.0/27,172.20.{0}.32/27,172.20.{0}.64/27,29,172.20.{0}.96/27,,,,172.21.{0}.0/27,172.21.{0}.32/27,172.21.{0}.64/27,29,172.21.{0}.96/27
GW:,,172.20.{0}.1,172.20.{0}.33,172.20.{0}.65,,172.20.{0}.97,,GW:,,172.21.{0}.1,172.21.{0}.33,172.21.{0}.65,,172.21.{0}.97

This is the output:
,,,,,,,,,,,,,,

,,,,,,,,,,,,,,
GW:,,172.20.126.129,172.20.126.161,172.20.126.193,,172.20.126.225,,GW:,,172.21.126.129,172.21.126.161,172.21.126.193,,172.21.126.225

''''''''''''''''''

There are blank lines between and I am assuming that these are from newlines being introduced.

The idea of this script is/was to abstract the csv data so that I dont need to place them in the file as there are many seperate CSV files to import.

I hope I answered your questions.

The majority of the script is below.
#!/usr/bin/python

import sys, os
#import pdb

#pdb.set_trace()

f=open('europe_germanyfinalcsv')
lines=f.readlines()

BU = 'Europe Germany,,,,,,,,,,,,,,'
PPP = 'Pre-Prod,,,,,,,,Prod,,,,,,'
C1 = ',,,,,,,,,,,,,,'
Title = 'Site,Environment,vApp Web,vApp App,vApp Data,Hosts Per Net,Reserved,,Site,Environment,vApp Web,vApp App,vApp Data,Hosts Per Net,Reserved'
NET1 = lines[4]
GW1 = lines[5]
M1 = lines[6]
PS1 = lines[7]
PE1 = lines[8]


for count in range(64, 127):
print NET1.format(count)
print GW1.format(count)
print M1
print PS1.format(count)
print PE1.format(count)
 
D

Dave Angel

<SNIP massive double-spaced nonsense from googlegroups misuse,
see http://wiki.python.org/moin/GoogleGroupsPython >

Thanks for the tip about the CSV module. I did not know about that.

So why aren't you using it? There's not much point in solving "the
newlines problem" if you're going to later switch to a library which
doesn't have the problem. The csv module will eliminate the newlines,
and the commas, organizing your data for you directly (assuming this
really is a csv file, and that the separator is comma). None of your
present code seems to believe it's a csv file, so maybe that's just a
misdirection.
 
J

Jason Friedman

Here are two lines from the CSV file:
,,172.20.{0}.0/27,172.20.{0}.32/27,172.20.{0}.64/27,29,172.20.{0}.96/27,,,,172.21.{0}.0/27,172.21.{0}.32/27,172.21.{0}.64/27,29,172.21.{0}.96/27
GW:,,172.20.{0}.1,172.20.{0}.33,172.20.{0}.65,,172.20.{0}.97,,GW:,,172.21.{0}.1,172.21.{0}.33,172.21.{0}.65,,172.21.{0}.97

This is the output:
,,,,,,,,,,,,,,

,,,,,,,,,,,,,,
GW:,,172.20.126.129,172.20.126.161,172.20.126.193,,172.20.126.225,,GW:,,172.21.126.129,172.21.126.161,172.21.126.193,,172.21.126.225

''''''''''''''''''

When you say "this is the output" do you mean that is what you are
getting or that is what you want? If that is what you are getting
please reply with what you want for output.
 
D

Dennis Lee Bieber

When you say "this is the output" do you mean that is what you are
getting or that is what you want? If that is what you are getting
please reply with what you want for output.

Considering that, off hand, there is no viable way to match input

GW:,,172.20.{0}.1

to output

GW:,,172.20.126.129

except by assuming that those are not the output for the sample input.

The "first" sample input appears to be a comma separated list of IP
netmask definitions in which the third octet is a placeholder to be
filled in later, and the fourth octet defines the starting address of
each subnet.

The second input line appears to be a list if IPs, again with a
placeholder for the third octet, and in which the fourth octet is the
first "assignable" address in the subnet.

Both (input and output) seem to have the inconsistancy of:

a) Based on the GW appearing twice on a line it looks almost like there
are TWO records per line

b) Inexplicable gaps in the records, shown by the ,, pairs

Just from the samples, most of the output can be generated
algorithmically...

-=-=-=-=-=-
template = "172.%d.%d.%d"

out = ["GW:", ""]
for mnet in [ 20, 21, 22 ]:
for net in [ 126, 127 ]:
for snet in range(1, 255, 32):
if len(out) == 6:
out.extend(["", "GW:", ""])
out.append(template % (mnet, net, snet))
print ",".join(out)
out = ["GW:", ""]

if len(out) > 2:
print ",".join(out)

-=-=-=-=-=-
GW:,,172.20.126.1,172.20.126.33,172.20.126.65,172.20.126.97,,GW:,,172.20.126.129,172.20.126.161,172.20.126.193,172.20.126.225
GW:,,172.20.127.1,172.20.127.33,172.20.127.65,172.20.127.97,,GW:,,172.20.127.129,172.20.127.161,172.20.127.193,172.20.127.225
GW:,,172.21.126.1,172.21.126.33,172.21.126.65,172.21.126.97,,GW:,,172.21.126.129,172.21.126.161,172.21.126.193,172.21.126.225
GW:,,172.21.127.1,172.21.127.33,172.21.127.65,172.21.127.97,,GW:,,172.21.127.129,172.21.127.161,172.21.127.193,172.21.127.225
GW:,,172.22.126.1,172.22.126.33,172.22.126.65,172.22.126.97,,GW:,,172.22.126.129,172.22.126.161,172.22.126.193,172.22.126.225
GW:,,172.22.127.1,172.22.127.33,172.22.127.65,172.22.127.97,,GW:,,172.22.127.129,172.22.127.161,172.22.127.193,172.22.127.225
 

Members online

No members online now.

Forum statistics

Threads
474,138
Messages
2,570,803
Members
47,348
Latest member
nethues

Latest Threads

Top