+ 1
Print each characterâs ASCII values of string
My problem is in the title... I write : sys.argv=« ./tentative.py arg1 » len(sys.argv) s=str(sys.argv[1]) l1=[len(s)] for c in s: l1.append(ord(c)) print(l1) I run : ./tentative g My output is : [1, 47] But I would like to have : 103, 97, 122 The ASCII value of each char of the str. Does anyone know what is the problem, please?
3 Answers
+ 6
If I understood you correctly 103, 97, 122 should be generated if the command line argument was "gaz"? the 47 code was for uppercase "G". The first item of the 'l1' list was the string length as per your code l1 = [len(s)], so unless you remove the len(s) you will get the string length as the first item in list 'l1'.
You wrote;
"I run : ./tentative g"
Here you only pass a one character length string ("g") but the ord() outputs 47, did you type "G" instead of "g" possibly?
(Edit)
Try to print the <s> string to verify you have the command line argument correctly.
+ 3
Siyam what do you get if you print the command line argument directly, or if you print the <s> variable content? do you get the argument correctly? also try to print the len(sys.argv) to check how many arguments were exactly read by the code.
I'm a fool for Linux system, I hope someone more proficient can drop a word or two here : )
+ 1
Ipang yes exactly.
the fact is, I am trying to print each ascii value of a string passed on arguments of the command-ligne as argv1
./tent.py argv1
when I define a string on my code it works; I get as Output the ascii values like I want.
But when I try do the same but print ascii value of an str non-known passed as argument in the command-line it doesnât work