I am writing a program that changes ascii input to text. That itself is not hard. My problem is this, the format for the ascci input is rather weird.
42$28$50$65$79$62$77$37$56$
Now, I can put the string into a list, and have it ignore the special characters. However, It breaks up the numbers like this:
[4,2,2,8,5,0,6,5,7,9,6,2,7,7,3,7,5,6]
Is there a way to change the input string into a list that is formatted like this?:
[42,28,50,65,79,62,77,37,56]
In other words, keeping the original format, while ignoring the special characters.
3 1491 bvdet 2,851
Expert Mod 2GB
I am writing a program that changes ascii input to text. That itself is not hard. My problem is this, the format for the ascci input is rather weird.
42$28$50$65$79$62$77$37$56$
Now, I can put the string into a list, and have it ignore the special characters. However, It breaks up the numbers like this:
[4,2,2,8,5,0,6,5,7,9,6,2,7,7,3,7,5,6]
Is there a way to change the input string into a list that is formatted like this?:
[42,28,50,65,79,62,77,37,56]
In other words, keeping the original format, while ignoring the special characters.
- >>> s = '42$28$50$65$79$62$77$37$56$'
-
>>> s.split('$')
-
['42', '28', '50', '65', '79', '62', '77', '37', '56', '']
-
>>> s.strip('$').split('$')
-
['42', '28', '50', '65', '79', '62', '77', '37', '56']
-
>>> [i for i in s.split('$') if i]
-
['42', '28', '50', '65', '79', '62', '77', '37', '56']
-
>>>
- >>> s = '42$28$50$65$79$62$77$37$56$'
-
>>> s.split('$')
-
['42', '28', '50', '65', '79', '62', '77', '37', '56', '']
-
>>> s.strip('$').split('$')
-
['42', '28', '50', '65', '79', '62', '77', '37', '56']
-
>>> [i for i in s.split('$') if i]
-
['42', '28', '50', '65', '79', '62', '77', '37', '56']
-
>>>
Thanks, I actually figured it out right before reading this. Found a new problem lol.
Here is the code I am using: -
import os
-
import string
-
string = raw_input('Text to decode: ')
-
shift = raw_input('Shift is: ')
-
string = string.split('%')
-
string = string[0:-1]
-
shift = int(shift)
-
x = -1
-
x = int(x)
-
shift = shift * x
-
newlist = []
-
newlist2 = []
-
for each in string:
-
each = int(each)
-
each = each + shift
-
newlist.append(each)
-
for chara in newlist:
-
chara = chr(chara)
-
newlist2.append(chara)
-
print newlist2
-
This only works if '$' is used to separate the ascii.
I tried: - bad = string.letters + '_' + '-' + '[' + ']' + '{' + '}' + '"' + ':' + ';' + '<' + '>' + '?' + '/' + '!' + '@' + '#' + '$' + '%' + '^' + '&' + '*' + '(' + ')'
-
string = string.split(bad)
However, that didn't work. Is there away to remove everything except integers from the string?
I really do appreciate your help. Thank you.
bvdet 2,851
Expert Mod 2GB
Thanks, I actually figured it out right before reading this. Found a new problem lol.
Here is the code I am using: -
import os
-
import string
-
string = raw_input('Text to decode: ')
-
shift = raw_input('Shift is: ')
-
string = string.split('%')
-
string = string[0:-1]
-
shift = int(shift)
-
x = -1
-
x = int(x)
-
shift = shift * x
-
newlist = []
-
newlist2 = []
-
for each in string:
-
each = int(each)
-
each = each + shift
-
newlist.append(each)
-
for chara in newlist:
-
chara = chr(chara)
-
newlist2.append(chara)
-
print newlist2
-
This only works if '$' is used to separate the ascii.
I tried: - bad = string.letters + '_' + '-' + '[' + ']' + '{' + '}' + '"' + ':' + ';' + '<' + '>' + '?' + '/' + '!' + '@' + '#' + '$' + '%' + '^' + '&' + '*' + '(' + ')'
-
string = string.split(bad)
However, that didn't work. Is there away to remove everything except integers from the string?
I really do appreciate your help. Thank you.
Assuming you want to remove the non-digit characters from the string: - import re
-
import string
-
-
patt = re.compile(r'\d+')
-
-
s = '42$28$50$65$79$62$77$37$56$'
-
s1 = ''.join(patt.findall(s))
-
print '%s -----> %s' % (s, s1)
-
-
s2 = ''.join([i for i in s if i in string.digits])
-
print s2
Output: - >>> 42$28$50$65$79$62$77$37$56$ -----> 422850657962773756
-
422850657962773756
-
>>>
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
14 posts
views
Thread by Brandon Hoppe |
last post: by
|
10 posts
views
Thread by Elizabeth Harmon |
last post: by
|
4 posts
views
Thread by Warren Sirota |
last post: by
|
28 posts
views
Thread by Siv |
last post: by
|
1 post
views
Thread by Rahul |
last post: by
|
4 posts
views
Thread by Mohan |
last post: by
|
3 posts
views
Thread by sparks |
last post: by
|
reply
views
Thread by Jack Wu |
last post: by
| |
29 posts
views
Thread by Barry |
last post: by
| | | | | | | | | | |