473,473 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

simple string formatting question

I have a list of strings (sys.argv actually). I want to print them as a
space-delimited string (actually, the same way they went into the command
line, so I can cut and paste)

So if I run my program like:
../my_prog a b c d

I want it to print:

'./my_prog' 'a' 'b' 'c' 'd'

Just print sys.argv will almost work, but it's comma-delimited.

There must be some clever way to do this. Any ideas?

Dec 14 '07 #1
2 1063
Neal Becker a écrit :
I have a list of strings (sys.argv actually). I want to print them as a
space-delimited string (actually, the same way they went into the command
line, so I can cut and paste)

So if I run my program like:
./my_prog a b c d

I want it to print:

'./my_prog' 'a' 'b' 'c' 'd'
This should do what you want:

print " ".join("'%s'" % arg for arg in sys.argv)

NB : if your version of Python predates generator expressions, use a
list comp instead:

print " ".join(["'%s'" % arg for arg in sys.argv])
Dec 14 '07 #2
On 2007-12-14, Neal Becker <nd*******@gmail.comwrote:
I have a list of strings (sys.argv actually). I want to print them as a
space-delimited string (actually, the same way they went into the command
line, so I can cut and paste)

So if I run my program like:
./my_prog a b c d

I want it to print:

'./my_prog' 'a' 'b' 'c' 'd'

Just print sys.argv will almost work, but it's comma-delimited.

There must be some clever way to do this. Any ideas?
The csv module is clever. Try the following:

import sys
import csv

writer = csv.writer(sys.stdout, delimiter=' ', quotechar="'",
quoting=csv.QUOTE_ALL)
writer.writerow(sys.argv)

You might want to set a few more of the dialect options, too,
e.g., in case an arg contains a '.

The shutil module might contain something more specialized.

--
Neil Cerutti
Dec 14 '07 #3

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

Similar topics

7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
8
by: Tony | last post by:
Hello I am learning C# and encountered the following problem when I tried to figure out how to print the string {0} in a Console window The following piece of codes complied OK. But when I...
3
by: The Eeediot | last post by:
How do I get a number like "131" to appear in a currency format like "$131.00" in a TextBox control? I know it's simple but I cannot find how to do this in any reference manual. TIA....
5
by: Burak | last post by:
Hello, I would like to format the string "11304200" into "11-3042.00". Can I do this with String.Format method? I have not come across any good documentation. Thank you,
6
by: alexrussell101 | last post by:
For anyone who can't be bothered to read my code and examples, scroll to the bottom, the question's there. Thanks. I'm using php and regular expressions to convert bbcode style things to html....
7
by: Steven D'Aprano | last post by:
I have a sinking feeling I'm missing something really, really simple. I'm looking for a format string similar to '%.3f' except that trailing zeroes are not included. To give some examples: ...
8
by: rdrink | last post by:
I am just getting into pysqlite (with a fair amount of Python and MySQL experience behind me) and have coded a simple test case to try to get the hang of things... yet have run into a 'stock...
11
by: Dustan | last post by:
Is there any builtin function or module with a function similar to my made-up, not-written deformat function as follows? I can't imagine it would be too easy to write, but possible... 'I am...
2
by: RichB | last post by:
I am attempting to output a trace with details of the db id of a price using: Trace.WriteLine("price id(first price): {0}",evnt.Prices.Id); Wher the Id is a string. I expect from this to...
0
by: opedog | last post by:
I'm monkeying around with databinding, trying to learn all the ins and outs. I'm populating a label from one of my business objects properties which is a string, however, I want some formatting done...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.