Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Basic Syntax Question, switching from Perl to Python

Posted on 2006-07-18
10
Medium Priority
?
310 Views
Last Modified: 2010-04-16
How would I write a Python statement that is equivalent to this Perl statement?

my ($field1, $field2, $field3) = split( /\t/,  tabDelimitedFields )


Is there a way to do it in a single line, or at least in fewer than 4 lines?

Thanks!
0
Comment
Question by:BerkeleyJeff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 29

Accepted Solution

by:
pepr earned 2000 total points
ID: 17129172
tabDelimitedFields = 'data1\tdata2\tdata3'
field1, field2, field3 = tabDelimitedFields.split('\t')
print field1
print field2
print field3
0
 
LVL 29

Expert Comment

by:pepr
ID: 17129190
But you may prefer to build a list of field data instead of putting them into the variables:

lst = tabDelimitedFields.split('\t')

print lst
print len(lst)
print lst[0]
print lst[1]
print lst[2]

0
 
LVL 29

Expert Comment

by:pepr
ID: 17129232
With the list, you can easily iterate through all items in the loop:

for item in lst:
    print item

The last note, you can use also the optional argument of the split() method that says how many times the source may be splitted:

field1, field2 = tabDelimitedFields.split('\t', 1)
print field1
print field2

The field2 will contain 'data2\tdata3' in this case.
0
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
LVL 29

Expert Comment

by:pepr
ID: 17129281
If you want to use the separator defined by a regular expression, then you may use the module re and the function findall() from the module.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 17129481
pepr: Did you mean re.split()?

>>> import re
>>> re.split('\t', 'data1\tdata2\tdata3')
['data1', 'data2', 'data3']
0
 
LVL 29

Expert Comment

by:pepr
ID: 17130035
No. I ment the split() of the built-in string -- 2.3.6.1 String Methods in the doc. All of the examples were tested with Python 2.4.3.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 17130237
I was referring to this: "If you want to use the separator defined by a regular expression, then you may use the module re and the function findall() from the module."  I believe re.split() rather than re.findall() is what Jeff would need to use here.
0
 
LVL 29

Expert Comment

by:pepr
ID: 17136037
To RichieHinde: Sorry for my last comment. Looking at your score, I should think faster and type slower next time ;-) Yes, if the separator should be the regular expression, then re.split() should be used.

Still, if the separator is '\t' or any explicit character sequence then the split() of the built-in string type is better -- in my opinion.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 17136412
pepr: "if the separator is '\t' or any explicit character sequence then the split() of the built-in string type is better" - agreed 100%!
0
 

Author Comment

by:BerkeleyJeff
ID: 17136614
Thanks! I'm now well on my way to leaving Perl for Python.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question