473,398 Members | 2,812 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,398 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 4080
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.