Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

suppress newlines in my script

16 views
Skip to first unread message

sloa...@gmail.com

unread,
May 23, 2013, 4:49:02 PM5/23/13
to
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)

Message has been deleted

sloa...@gmail.com

unread,
May 24, 2013, 9:59:43 AM5/24/13
to
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]


Dave Angel

unread,
May 24, 2013, 3:24:26 PM5/24/13
to pytho...@python.org
On 05/24/2013 09:59 AM, sloa...@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

Jason Friedman

unread,
May 24, 2013, 11:49:24 PM5/24/13
to sloa...@gmail.com, python-list
> 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.
Message has been deleted
0 new messages