Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45843 > unrolled thread
| Started by | sloan949@gmail.com |
|---|---|
| First post | 2013-05-23 13:49 -0700 |
| Last post | 2013-05-25 13:33 -0400 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.python
suppress newlines in my script sloan949@gmail.com - 2013-05-23 13:49 -0700
Re: suppress newlines in my script Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-23 19:11 -0400
Re: suppress newlines in my script sloan949@gmail.com - 2013-05-24 06:59 -0700
Re: suppress newlines in my script Dave Angel <davea@davea.name> - 2013-05-24 15:24 -0400
Re: suppress newlines in my script Jason Friedman <jsf80238@gmail.com> - 2013-05-24 21:49 -0600
Re: suppress newlines in my script Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-25 13:33 -0400
| From | sloan949@gmail.com |
|---|---|
| Date | 2013-05-23 13:49 -0700 |
| Subject | suppress newlines in my script |
| Message-ID | <52c74908-8bac-498e-9549-5b9500b152f1@googlegroups.com> |
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)
[toc] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-05-23 19:11 -0400 |
| Message-ID | <mailman.2043.1369350733.3114.python-list@python.org> |
| In reply to | #45843 |
On Thu, 23 May 2013 13:49:02 -0700 (PDT), sloan949@gmail.com declaimed
the following in gmane.comp.python.general:
> 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?
<snip>
>
>
> 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.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | sloan949@gmail.com |
|---|---|
| Date | 2013-05-24 06:59 -0700 |
| Message-ID | <b52fcbed-3ea6-4352-99de-c6274c557016@googlegroups.com> |
| In reply to | #45843 |
On Thursday, May 23, 2013 1:49:02 PM UTC-7, sloa...@gmail.com wrote:
> 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)
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-05-24 15:24 -0400 |
| Message-ID | <mailman.2074.1369423480.3114.python-list@python.org> |
| In reply to | #45893 |
On 05/24/2013 09:59 AM, sloan949@gmail.com wrote:
>
<SNIP massive double-spaced nonsense from googlegroups misuse,
see http://wiki.python.org/moin/GoogleGroupsPython >
>> print PE2.format(count)
>
> 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.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Jason Friedman <jsf80238@gmail.com> |
|---|---|
| Date | 2013-05-24 21:49 -0600 |
| Message-ID | <mailman.2103.1369453767.3114.python-list@python.org> |
| In reply to | #45893 |
> 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.
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-05-25 13:33 -0400 |
| Message-ID | <mailman.2145.1369503203.3114.python-list@python.org> |
| In reply to | #45893 |
On Fri, 24 May 2013 21:49:24 -0600, Jason Friedman <jsf80238@gmail.com>
declaimed the following in gmane.comp.python.general:
> > 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.
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
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web