473,326 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 1540
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

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Brandon Hoppe | last post by:
I'm trying to change the src of an ilayer in the parent document from a link inside the ilayer. I'm not able to get it to work. All that happens is Netscape 4 crashes. This is for Netscape 4 only....
10
by: Elizabeth Harmon | last post by:
Hi All I am attempting to open a Word App from a web page on the client and so far everything works (After reconfig of dcomcnfg for Microsoft Word Document). I have one minor problem, i cannot...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
28
by: Siv | last post by:
Hi, If I run the following: strSQL = "Select * FROM Clients;" da = New OleDb.OleDbDataAdapter(strSQL, Conn) 'Create data adapter cb = New OleDb.OleDbCommandBuilder(da) ...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
4
by: Mohan | last post by:
In the database, we are storing time in 3 columns: hours, Minutes and AM,PM. After retrieving it from the database, I want to load these values into a DateTime variable. I haven't had much...
3
by: sparks | last post by:
I have 2 fields that I have to make sure don't get screwed up. (could be one it doesn't matter) but what they want is no duplicates, warn the user changing existing numbers is a no no. (of course...
0
by: Jack Wu | last post by:
Hi I've spent a good majority of my day trying to figure out how to have PIL 1.1.5 working on my OSX 10.3.9_PPC machine. I'm still stuck and I have not gotten anywhere. Could somebody please...
0
by: yjh0914 | last post by:
hi guys! so im basically editting my post i made earlier as it wznt as specific.. i have to make a program that basically ranks students by their cumulative gpa. the student's info is on a csv file...
29
by: Barry | last post by:
I know this is not a php question. If that bothers you, don't respond. If not, I sure could use the advice... I'm using a very abbreviated set of code to show a calendar. The idea is to simply...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.