473,383 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Avoid newline at the end

Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the second
line.
How to do that most elegantly with Python?

Thanks,

Florian
Nov 11 '07 #1
4 1972
On Nov 11, 10:22 am, Florian Lindner <Florian.Lind...@xgm.dewrote:
Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the second
line.
How to do that most elegantly with Python?
Naively after your code...
logs = logs.rstrip()

But probably, I'd have constructed the list of logs, then used 'join'
to build the string.

logs = []
for row in resultSet:
for name in ('access.log', 'error.log'):
logs += ['/home/%s/%s/log/%s' % (row[1], row[0], name)]
logs = '\n'.join(logs)

Or equivalently using a list comprehension...

logs = '\n'.join('/home/%s/%s/log/%s' % (row[1], row[0], name)
for row in resultSet
for name in ('access.log', 'error.log'))

--
Paul Hankin

Nov 11 '07 #2
On Sun, 11 Nov 2007 11:22:19 +0100, Florian Lindner wrote:
Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the
second line.
That means your log file doesn't end with a newline. That's often not
good, because it can confuse some tools.

Also, appending lots of strings together like that is very inefficient.
How to do that most elegantly with Python?
If you have a small number of rows (say, less than a few tens of
thousands), you can do this:

rows = []
for row in resultSet:
rows.append("/home/%s/%s/log/access.log" % (row[1], row[0]))
rows.append("/home/%s/%s/log/error.log" % (row[1], row[0]))
# note that there are no newlines
logs = '\n'.join(rows) # do it once at the end

But again, when you write text to a file, you should end it with a
newline. It isn't compulsory, but it is best practice.

Alternatively, check out the logging module.

--
Steven.
Nov 11 '07 #3
Florian Lindner <Fl*************@xgm.dewrites:
for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

def logfile_path(name, row):
return "/home/%s/%s/log/%s " % (row[1], row[0], name)

logs = '\n'.join(logfile_path(name, row)
for row in resultSet
for name in ('access.log', 'error.log'))
Nov 11 '07 #4
Steven D'Aprano wrote:
On Sun, 11 Nov 2007 11:22:19 +0100, Florian Lindner wrote:
>Hello,
I have a piece of code like that:

for row in resultSet:
logs += "/home/%s/%s/log/access.log \n" % (row[1], row[0])
logs += "/home/%s/%s/log/error.log \n" % (row[1], row[0]) # <--

Now I want to avoid the newline at the last iteration and only at the
second line.

That means your log file doesn't end with a newline. That's often not
good, because it can confuse some tools.

Also, appending lots of strings together like that is very inefficient.
>How to do that most elegantly with Python?

If you have a small number of rows (say, less than a few tens of
thousands), you can do this:

rows = []
for row in resultSet:
rows.append("/home/%s/%s/log/access.log" % (row[1], row[0]))
rows.append("/home/%s/%s/log/error.log" % (row[1], row[0]))
# note that there are no newlines
logs = '\n'.join(rows) # do it once at the end

But again, when you write text to a file, you should end it with a
newline. It isn't compulsory, but it is best practice.

Alternatively, check out the logging module.
That is not log file it's a config file for logrotate. And the log string
goes into a template therefore the config file ends with a newline. The
problem is that logrotate gets confused by empty lines between logfile path
and config.
The number of lines will always be < 100 and so config will only be
regenerated not often so efficiency is not issue.
Regards,

Florian
Nov 11 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
9
by: Alan Mackenzie | last post by:
To all those who use (X)Emacs's CC Mode to edit C, C++, Java, Objective-C, Pike, AWK or IDL: To help direct the development of CC Mode, it would be useful to find out how people use the...
29
by: runningdog | last post by:
Hi, I would like to be able to embed a newline in a text string. Is there any convienent notation to do this TIA Steve
0
by: davidmichaelkarr | last post by:
I'm using WebLogic Scripting Tool, which uses Jython, which uses Python 2.1. In a script I'm writing, executing a "cd()" always emits a newline to stdout. Is there a way to make it not emit that...
4
by: Peter Kirk | last post by:
Hi I would like to ask a little bit about the value Environment.Newline: what is it and what is the point of it? Ok, I can see in the docs that it represents "newline" for the current platform -...
5
by: Adam Right | last post by:
Hi, Is there a way to construct the mail body including newline characters by using .net framework mailing functions when sending an email? I cannot insert newline character into the body of the...
11
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const...
1
by: linq936 | last post by:
Hi, I read in many places that the string to be outputted by printf() must be ending with newline, for example, it should be printf("Hello World.\n"); instead of printf("Hello World.");
4
by: seberino | last post by:
print "foo" print "bar" has a newline in between "foo" and "bar" print "foo", print "bar" has a space in between "foo" and "bar"
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.