473,382 Members | 1,622 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,382 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 4079
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.