-2

EDIT: I know there were previous similar questions. Except that it was asked by people who already know Python. I did try the duplicated answer you suggested and it is not working (nothing display in console when I ran it with command line argument). That solution seems to ignore input value via cmd line arg. I could do the job easily in other languages. But for this exercise, I need a Python script. Please help me to write a script ready for use. Sorry if this sounds like a careless request. I do know programming, the trouble here is that I don't know anything about Python.

This is to be used in a streaming exercise with the Hive language (part of Hadoop). Here is the specs:

  • The script take the value from the command line argument and return the result to standard output
  • Add thousand separator when the value is compatible with numerical value, otherwise re-output the same input value unchanged.

Example:

$ InsertThousandSeparator.py 386
386

$ InsertThousandSeparator.py 1234567
1,234,567

$ InsertThousandSeparator.py 123ABC
123ABC

$ InsertThousandSeparator.py 123ABC456
123ABC456

$ InsertThousandSeparator.py Hello
Hello

$ InsertThousandSeparator.py 12345.67
12,345.67

The last example with decimal, if it's too complicate to code, this could be skipped.

Thank you very much in advance for any help.

Polymerase
  • 6,311
  • 11
  • 47
  • 65
  • Am I reading this wrong or is the first input/output wrong? – A.J. Uppal May 15 '14 at 04:49
  • Are those the *only* cases that the program should handle? Or should the program work with `12345ABC12345 -> 12,345ABC12,345` etc? Anyway for a start you can use the `,` format specifier: `'{:,.2f}'.format(12345.67)` is `'12,345.67'`. – Bakuriu May 15 '14 at 04:52
  • @aj8uppal, you are right, I have made correction. – Polymerase May 15 '14 at 15:12
  • @Bakuriu 12345ABC12345 is not a number so must be re-output as 12345ABC12345. If not too much demanding guys, please help me to write that script READY for use b/c I don't know Python syntax. If you are interested, I can offer you the same solution in Java, C# or Powershell in exchange. – Polymerase May 15 '14 at 15:17
  • 1
    This is not really how StackOverflow works. This is a site designed to help fellow programmers with specific problems; "Please write my code for me because I have no idea what to do"-questions are frowned upon. Your question appears to contain three questions, the most relevant one being "how to insert thousands separators", and that is thoroughly answered by the linked question. If your other questions are "how to handle errors in Python", you might want to look into [exceptions](https://docs.python.org/2/tutorial/errors.html#handling-exceptions),... – Tim Pietzcker May 15 '14 at 18:19
  • ...and for the question "how to read command line arguments", there are many answers here as well, for example http://stackoverflow.com/q/1009860/20670. I take it that printing to `stdout` shouldn't be a problem... – Tim Pietzcker May 15 '14 at 18:24
  • It looks like a duplicate of [how to print number with commas as thousands separators in Python 2.x](http://stackoverflow.com/q/1823058/12892) – Cristian Ciupitu May 15 '14 at 20:00

1 Answers1

2

OK, here you are - a script for Python 2.7. But please try to ask one question at a time next time, OK?

import sys
arg = sys.argv[1]                  # get first command line parameter
if arg.isdigit():                  # integer value
    value = int(sys.argv[1])   
else:
    try:
        value = float(sys.argv[1]) # try float conversion
    except ValueError:             # if that fails
        value = None               # mark value as unusable
if value is not None:
    print "{:,}".format(value)     # print with thousands separator
else:                              # or, if not a number
    print arg                      # print as-is.
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Awesome, works perfect. Thank you very much. Sorry I know I was asking a lame question. If I had a choice of language I would do the job even in my sleep. Fortunately, this is likely the only time I need to ask a Python question. However, note taken. I will not spill my total ignorance next time on StackOverflow. – Polymerase May 15 '14 at 20:16
  • BTW, Your help is appreciated Tim. In my little exercise, I put in comment a credits / thank you note that you are the author of the Python script. – Polymerase May 15 '14 at 20:23