473,287 Members | 1,866 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,287 software developers and data experts.

using .split ( )?

Thekid
145 100+
Hi,

Is there a way for me to use .split without having to constantly change my code to adapt to a string? Here's a sample:

Expand|Select|Wrap|Line Numbers
  1.  
  2. s =input("Enter string:")
  3. #say string entered is: "2/3/4/5/6"
  4. x=s.split("/")
  5. print x
  6.  
  7.  
That works fine but how can I get it to handle any string that is entered if on the next run it looks like one of these:

"2#3#4#5#6"
or
"45%65%74%2%9%"
etc.....
Oct 26 '07 #1
3 4077
oler1s
671 Expert 512MB
There's a couple of ways. The first is to have the user specify what the delimiting value is. This is the most straightforward, and probably the best way. The second is to take a guess, by manipulating the string yourself, and seeing what the pattern might look like.
Oct 26 '07 #2
bvdet
2,851 Expert Mod 2GB
You can use the re module. Try this:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. s = '1/2/3/4/5'
  4. s1 = '1#2#3#4#5'
  5. s2 = '1^2^3^4^5'
  6.  
  7. patt = '[/^#]'
  8.  
  9. print re.split(patt, s)
  10. print re.split(patt, s1)
  11. print re.split(patt, s2)
Oct 26 '07 #3
Thekid
145 100+
You can use the re module. Try this:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. s = '1/2/3/4/5'
  4. s1 = '1#2#3#4#5'
  5. s2 = '1^2^3^4^5'
  6.  
  7. patt = '[/^#]'
  8.  
  9. print re.split(patt, s)
  10. print re.split(patt, s1)
  11. print re.split(patt, s2)
Oler1s, thank you for your reply but this is more of what I was looking for:

Expand|Select|Wrap|Line Numbers
  1.  
  2. import re
  3.  
  4. patt = '[/&$^]'
  5. s = input("Enter string:")
  6. print re.split(patt,s)
  7.  
  8.  
Works good like that! Thanks Bvdet!
Oct 26 '07 #4

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

Similar topics

11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
0
by: mookie | last post by:
m looking to create something similar to #region and #endregion using treeviews the problem is that instead of #region, i am using ;fold and ;endfold i am also allowed ot have a fold within...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
11
by: pmarisole | last post by:
I am trying to use the vbscript "split" function on a multi-select field. I am trying to do a mass update of several records at a time. I am getting an error and I'm not sure what to do. Here is...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
2
by: David | last post by:
Hi, Could PHP be used to take a txt file (or set of txt files) and add a string of characters every X number of words or characters? Say a txt file with 50,000 characters/5,000 words how would...
1
by: Richard Harter | last post by:
On Fri, 27 Jun 2008 09:28:56 -0700 (PDT), pereges <Broli00@gmail.comwrote: There are some obvious questions that should be asked, e.g., is the contents of your array already sorted as your...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
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...

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.