By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

How do you read up to a specific character in a line of text?

P: 25
I have a line of characters that need to be separated. One example is like this:

513413;dialog_513413;Sally Mae has some jobs for you.;

Three sets of data all split into three groups placed in one line. What I would like to do is be able to read each group up to the semicolon (obviously without including it when it becomes displayed or used in the program).

So in conclusion I would just like to know a method that can either count or read the string (from a file) up to the semicolon. I am aware of string.read(size), but obviously size is needed and it is variable. Highly prefer having size given also.

Thanks! :)
Sep 6 '10 #1

✓ answered by bvdet

String method split() was made for situations like this.
Expand|Select|Wrap|Line Numbers
  1. >>> line = "513413;dialog_513413;Sally Mae has some jobs for you.;"
  2. >>> line.split(";")[0]
  3. '513413'
  4. >>> for item in line.split(";"):
  5. ...     print item
  6. ...     
  7. 513413
  8. dialog_513413
  9. Sally Mae has some jobs for you.
  10.  
  11. >>> 

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
String method split() was made for situations like this.
Expand|Select|Wrap|Line Numbers
  1. >>> line = "513413;dialog_513413;Sally Mae has some jobs for you.;"
  2. >>> line.split(";")[0]
  3. '513413'
  4. >>> for item in line.split(";"):
  5. ...     print item
  6. ...     
  7. 513413
  8. dialog_513413
  9. Sally Mae has some jobs for you.
  10.  
  11. >>> 
Sep 6 '10 #2

P: 25
Thanks again bvdet! :]
Sep 7 '10 #3

Post your reply

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