ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Looks like the "," operator in the print inside the loop is adding a space for its own.
I want to print the sequence WITHOUT spaces. Something like this:
hi ghostdog74 !
the loop in the example, is, well, an example.
in fact I am iterating over rows returned from a sql query and the output is CSV formated.
The problem here is how to use print with the no newline inside the loop to build a row of data. (at the end of the loop I print a newline to just start another loop for the next row.
The extra spaces are unwanted because some colunms are strings (and must be exact strings, without extra spaces) and other columns are float numbers which the extra space cause the data to be interpreted as a text, not as a number, when it is loaded in another system.
Hi Celine, thanks, your solution works but it is kind of awkward isn't ? At this point, it is more a matter of doing the thing in proper way in first place than solving the problem itself.
Is my idea awkward? I really don't know - that's the first python code I have ever put together.
But, I question whether your solution in post #6 is 'less awkward' or 'the proper way'; I would think that the 'proper way' would not involve an external tool to remove the space - rather, the space wouldn't be present to begin with. Therefore, I prefer my way over post #6.
I suspect Sergei or Ghostdog or any number of other members have a better grasp that you or I evidently do, so maybe they will tell us what's the "best" way. I figure Ghostdog's second suggestion could be adapted to your original query also.
hi ghostdog74 !
the loop in the example, is, well, an example.
in fact I am iterating over rows returned from a sql query and the output is CSV formated.
But, I question whether your solution in post #6 is 'less awkward' or 'the proper way'; I would think that the 'proper way' would not involve an external tool to remove the space - rather, the space wouldn't be present to begin with. Therefore, I prefer my way over post #6.
Cheers!
Hey, don't get me wrong !
my the code in #6 is not a solution! It is not even a patch ! It is just how I manage to delivery the info other people need and have more time to solve the problem exclusively using python.
So, I still have the problem. I still don't known how to print inside a loop, without newlines and extras spaces.
Of course, like Sergei told, there is more than a way to do this and yours is so good as any other:
Code:
x= "123.45"
output= ""
for i in range (1,10):
output= output + x + ';'
print output
but the original problem remains: Using the "," operator with print, how to avoid the extra spaces....
Anyway, sorry by the "awkward" - it was not a good word after all....but it is not what I was looking for in first place.
"awkward" may yet turn out to be the right word! I'm interested in seeing what turns up though, if there's an exact solution to what you ask (and I'm sure there is).
but the original problem remains: Using the "," operator with print, how to avoid the extra spaces....
Python's print statement is "designed" that way, for simple visualization of your output. The "pythonic" way to do what you want is using str.join() like my second example. And I have been using it as well to print sql statement output so yes, do consider using that method. (there certainly is no need to call extra sed or tools)
that said, if you have Python 2.6+ (or Python 3+)
Code:
>>> from __future__ import print_function # for Python 2.6
>>> print( *[x for i in range(10)], sep=',')
123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45
Last edited by ghostdog74; 10-01-2010 at 10:47 AM.
Python's print statement is "designed" that way, for simple visualization of your output.
Ok. I understand that. Makes sense to me. Thanks for the enlightenment.
So I will use others ways not related to "print x," to achieve my goals, since "print x," will, "by design", print an extra space anyway.
I can't use the way you suggested because "x" is not a constant. Just to clarify:
Code:
import kinterbasdb as k
# create a connection to database
con = k.connect(dsn='host:database', user='USER', password='PASSWD')
# Create a Cursor object that operates in the context of Connection con:
cur = con.cursor()
# SQL statement
SQL="select f1, f2, f3, ..fn from table where several conditions;"
# execute the SQL query
cur.execute(SQL)
# interate over the records
out= "row title"
for (f1, f2, f3,...fn) in cur:
a= functionA (f1, f2, f3, ...fn)
b= functionB (f1, f2, f3, ...fn)
x= functionC (a, b)
out= out + ';' + x
print out
(you see now why I reduced the problem in the first place ? If I am using a database, or how the "x" is evaluated it is not important - just "print x," printing a extra space is the problem.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.