471,084 Members | 1,062 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Splitting a string

440 256MB
Hi ,

I have a string which is having tabs and space as delimiter.I would like to store the read data as list.

For Example :

Input:
strLine = ' 12\t 13 Top\t 100'

Ouput :

strList = ['12','13','Top','100']

How to handle the above case when we have the 'tab' and 'space' as delimiter?

Thanks
PSB
Jul 24 '07 #1
3 901
bvdet
2,851 Expert Mod 2GB
Hi ,

I have a string which is having tabs and space as delimiter.I would like to store the read data as list.

For Example :

Input:
strLine = ' 12\t 13 Top\t 100'

Ouput :

strList = ['12','13','Top','100']

How to handle the above case when we have the 'tab' and 'space' as delimiter?

Thanks
PSB
Expand|Select|Wrap|Line Numbers
  1. >>> import re
  2. >>> strLine = ' 12\t 13 Top\t 100'
  3. >>> strList = [i for i in re.split('[\t ]', strLine) if i != '']
  4. >>> strList
  5. ['12', '13', 'Top', '100']
  6. >>> 
Jul 24 '07 #2
ilikepython
844 Expert 512MB
Hi ,

I have a string which is having tabs and space as delimiter.I would like to store the read data as list.

For Example :

Input:
strLine = ' 12\t 13 Top\t 100'

Ouput :

strList = ['12','13','Top','100']

How to handle the above case when we have the 'tab' and 'space' as delimiter?

Thanks
PSB
A simple "split" does the job:
Expand|Select|Wrap|Line Numbers
  1. >>> strLine = ' 12\t    13   Top\t  100'
  2. >>> strList = strLine.split()    // seperates whitespace
  3. >>> print strList
  4. ['12','13','Top','100']
  5.  
Jul 25 '07 #3
bvdet
2,851 Expert Mod 2GB
A simple "split" does the job:
Expand|Select|Wrap|Line Numbers
  1. >>> strLine = ' 12\t    13   Top\t  100'
  2. >>> strList = strLine.split()    // seperates whitespace
  3. >>> print strList
  4. ['12','13','Top','100']
  5.  
Of course! But that's too simple. *red face*
Jul 25 '07 #4

Post your reply

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

Similar topics

6 posts views Thread by qwweeeit | last post: by
5 posts views Thread by fatted | last post: by
2 posts views Thread by Trint Smith | last post: by
20 posts views Thread by Opettaja | last post: by
2 posts views Thread by CharChabil | last post: by
6 posts views Thread by HMS Surprise | last post: by
2 posts views Thread by shadow_ | last post: by
4 posts views Thread by yogi_bear_79 | last post: by
37 posts views Thread by xyz | last post: by

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.