469,332 Members | 6,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

Help changing a string.

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.
Jan 9 '08 #1
3 1424
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.
Expand|Select|Wrap|Line Numbers
  1. >>> s = '42$28$50$65$79$62$77$37$56$'
  2. >>> s.split('$')
  3. ['42', '28', '50', '65', '79', '62', '77', '37', '56', '']
  4. >>> s.strip('$').split('$')
  5. ['42', '28', '50', '65', '79', '62', '77', '37', '56']
  6. >>> [i for i in s.split('$') if i]
  7. ['42', '28', '50', '65', '79', '62', '77', '37', '56']
  8. >>> 
Jan 9 '08 #2
Expand|Select|Wrap|Line Numbers
  1. >>> s = '42$28$50$65$79$62$77$37$56$'
  2. >>> s.split('$')
  3. ['42', '28', '50', '65', '79', '62', '77', '37', '56', '']
  4. >>> s.strip('$').split('$')
  5. ['42', '28', '50', '65', '79', '62', '77', '37', '56']
  6. >>> [i for i in s.split('$') if i]
  7. ['42', '28', '50', '65', '79', '62', '77', '37', '56']
  8. >>> 

Thanks, I actually figured it out right before reading this. Found a new problem lol.

Here is the code I am using:

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import string
  3. string = raw_input('Text to decode: ')
  4. shift = raw_input('Shift is: ')
  5. string = string.split('%')
  6. string = string[0:-1]
  7. shift = int(shift)
  8. x = -1
  9. x = int(x)
  10. shift = shift * x
  11. newlist = []
  12. newlist2 = []
  13. for each in string:
  14.     each = int(each)
  15.     each = each + shift
  16.     newlist.append(each)
  17. for chara in newlist:
  18.     chara = chr(chara)
  19.     newlist2.append(chara)
  20. print newlist2
  21.  

This only works if '$' is used to separate the ascii.
I tried:
Expand|Select|Wrap|Line Numbers
  1. bad = string.letters + '_' + '-' + '[' + ']' + '{' + '}' + '"' + ':' + ';' + '<' + '>' + '?' + '/' + '!' + '@' + '#' + '$' + '%' + '^' + '&' + '*' + '(' + ')'
  2. 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.
Jan 9 '08 #3
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:

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import string
  3. string = raw_input('Text to decode: ')
  4. shift = raw_input('Shift is: ')
  5. string = string.split('%')
  6. string = string[0:-1]
  7. shift = int(shift)
  8. x = -1
  9. x = int(x)
  10. shift = shift * x
  11. newlist = []
  12. newlist2 = []
  13. for each in string:
  14.     each = int(each)
  15.     each = each + shift
  16.     newlist.append(each)
  17. for chara in newlist:
  18.     chara = chr(chara)
  19.     newlist2.append(chara)
  20. print newlist2
  21.  

This only works if '$' is used to separate the ascii.
I tried:
Expand|Select|Wrap|Line Numbers
  1. bad = string.letters + '_' + '-' + '[' + ']' + '{' + '}' + '"' + ':' + ';' + '<' + '>' + '?' + '/' + '!' + '@' + '#' + '$' + '%' + '^' + '&' + '*' + '(' + ')'
  2. 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:
Expand|Select|Wrap|Line Numbers
  1. import re
  2. import string
  3.  
  4. patt = re.compile(r'\d+')
  5.  
  6. s = '42$28$50$65$79$62$77$37$56$'
  7. s1 = ''.join(patt.findall(s))
  8. print '%s -----> %s' % (s, s1)
  9.  
  10. s2 = ''.join([i for i in s if i in string.digits])
  11. print s2
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 42$28$50$65$79$62$77$37$56$ -----> 422850657962773756
  2. 422850657962773756
  3. >>> 
Jan 9 '08 #4

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
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
reply views Thread by Jack Wu | last post: by
29 posts views Thread by Barry | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.