Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
10-01-2010, 08:46 AM
|
#1
|
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,392
Rep:
|
python: how to avoid spaces ?
Hi,
in the code below:
Code:
x= "123.45"
for i in range (1, 10):
print "%s;" % x,
print
The output is:
Code:
123.45; 123.45; 123.45; 123.45; 123.45; 123.45; 123.45; 123.45; 123.45;
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:
Code:
123.45;123.45;123.45;123.45;123.45;123.45;123.45;123.45;123.45;
but I couldn't figure out how to do this.....
How to print a sequence, inside a loop, without that space ?
|
|
|
|
10-01-2010, 09:07 AM
|
#2
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
you don't need the for loop
Code:
>>> x="123.45"
>>> ((x +",")*10)[:-1]
'123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45'
or list comprehension
Code:
>>> ','.join([ x for i in range(10) ])
'123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45'
|
|
|
|
10-01-2010, 09:28 AM
|
#3
|
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,392
Original Poster
Rep:
|
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.
Last edited by marozsas; 10-01-2010 at 09:30 AM.
|
|
|
|
10-01-2010, 09:29 AM
|
#4
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
I don't program in python so forgive me if this is a less than divine way of doing this:
Code:
#!/usr/bin/python
import sys
x= "123.45"
for i in range (1, 10):
sys.stdout.write (x); print ";",
print
I got the hint from here: http://bytes.com/topic/python/answer...t-ending-comma
|
|
|
1 members found this post helpful.
|
10-01-2010, 09:48 AM
|
#5
|
|
Senior Member
Registered: May 2005
Posts: 4,396
|
Quote:
Originally Posted by ghostdog74
you don't need the for loop
Code:
>>> x="123.45"
>>> ((x +",")*10)[:-1]
'123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45'
or list comprehension
Code:
>>> ','.join([ x for i in range(10) ])
'123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45,123.45'
|
OMG !!! What do I see in Python ?! There is more than way to do it ! 
|
|
|
|
10-01-2010, 09:56 AM
|
#6
|
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,392
Original Poster
Rep:
|
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.
In fact, what I already did is:
Code:
myprogram.py | sed -e 's/; /;/g' > output.csv
|
|
|
|
10-01-2010, 10:07 AM
|
#7
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Hi!
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.
Cheers!
|
|
|
|
10-01-2010, 10:11 AM
|
#8
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by marozsas
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.
|
show examples, and show desired output!
|
|
|
|
10-01-2010, 10:18 AM
|
#9
|
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,392
Original Poster
Rep:
|
Quote:
Originally Posted by GrapefruiTgirl
Hi!
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.
Last edited by marozsas; 10-01-2010 at 12:55 PM.
|
|
|
|
10-01-2010, 10:19 AM
|
#10
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by Sergei Steshenko
OMG !!! What do I see in Python ?! There is more than way to do it ! 
|
You are hopelessly misguided. These are not different ways to use a syntax. Not like Perl. So many for loop syntax, so many open() syntax. etc
|
|
|
|
10-01-2010, 10:20 AM
|
#11
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
No problem
"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).
|
|
|
|
10-01-2010, 10:22 AM
|
#12
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by GrapefruiTgirl
x= "123.45"
for i in range (1, 10):
sys.stdout.write (x); print ";",
print
[/code]
|
you will end up with a ";" at the end.
|
|
|
|
10-01-2010, 10:23 AM
|
#13
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
Quote:
Originally Posted by ghostdog74
you will end up with a ";" at the end.
|
Yes, thanks! That part I did figure out. 
|
|
|
|
10-01-2010, 10:26 AM
|
#14
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
Quote:
Originally Posted by marozsas
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.
|
|
|
1 members found this post helpful.
|
10-01-2010, 12:44 PM
|
#15
|
|
Senior Member
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,392
Original Poster
Rep:
|
Quote:
Originally Posted by ghostdog74
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.)
Anyway, it was fun, as always. 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:16 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|