-1

I'm new to Python, I apologize if the question is simple. I have a string called decShow with this informartion:

 00:00:00,000 -> 94 FB
 00:00:00,100 -> 94 FA
 00:00:00,200 -> 94 F9
 00:00:00,300 -> 94 F8
 00:00:00,400 -> 94 F7
 00:00:00,500 -> 94 F6
 00:00:00,600 -> 94 F5
 00:00:00,700 -> 94 F4
 00:00:00,800 -> 94 F3
 00:00:00,900 -> 94 F2
 00:00:01,000 -> 94 F1
 00:00:01,100 -> 94 20

HH:MM:SS,sss -> Hex Code

I want to modify that string line by line, creating another string like this

00000000: Hex Code

where the zeros are a HEX value resulting from the conversion of each line minutes, seconds and miliseconds to miliseconds in hexadecimal. Maths are not the problem, the problem is the loop. After trying with a for line in decShow I'm not having the results I expected.

How can I efficiently do that?

EDIT: To clarify.

I have this in a string:

 00:00:00,000 -> 94 FB
 00:00:00,100 -> 94 FA
 00:00:00,200 -> 94 F9
 00:00:00,300 -> 94 F8

I want to transform it into this:

00000000: 94 FB
00000064: 94 FA
000000C8: 94 F9
0000012C: 94 F8

I tried a loop under for line in decShow but it seems it works just for .readlines()

Lorum
  • 3
  • 3

5 Answers5

1

You will need to split the string into line using str.splitlines() or .split('\n') and then strip out the characters that you dont want.

x = "\n".join([line.strip("->").strip(",") for line in decShow.splitlines()])
Wamadahama
  • 1,489
  • 13
  • 19
0

for line in decShow treats decShow as a list of characters. You want to have a list of all the lines in decShow which is easily reachable using decShow.split("\n"), and then for line in decShow.split("\n") will give each line the value of a single line in decShow.

Neo
  • 3,534
  • 2
  • 20
  • 32
  • (str.splitlines)[https://docs.python.org/2/library/stdtypes.html#str.splitlines] will be more robust here. – Ben Jones Jul 20 '18 at 13:22
0

If decShow is a str, then your for loop will loop over characters, not lines. In order to loop over lines separated by newline characters, try using str.splitlines:

for line in decShow.splitlines():
    // convert line to your Hex representation here
Ben Jones
  • 652
  • 6
  • 21
0

You can use decShow.split("\n") and it will split the string into multiple strings by the new line character. You can also use the same thing for example if you wanted to split that first part by commas decShow.split(",")

In addition, I think for this problem you're also going to need to do some substrings depending on how you go about it. You can read more about that elsewhere...

How to display the first few characters of a string in Python?

Carson
  • 1,147
  • 5
  • 19
  • 41
0
>>> import re
>>> tup_lst = [re.split(r' -> |,', l.strip()) for l in decShow.splitlines()]
>>> new_data = '\n'.join([a.replace(':', '')+'%03X: ' % int(b)+c for a,b,c in tup_lst])
>>> print (new_data)

Output

000000000: 94 FB
000000064: 94 FA
0000000C8: 94 F9
00000012C: 94 F8
000000190: 94 F7
0000001F4: 94 F6
000000258: 94 F5
0000002BC: 94 F4
000000320: 94 F3
000000384: 94 F2
000001000: 94 F1
000001064: 94 20
Sunitha
  • 11,777
  • 2
  • 20
  • 23
  • All answers were useful, but this solution is by far the most efficient. Thank you very much. That really helped me, you learned me new things. – Lorum Jul 20 '18 at 20:34