Solved

Basic Syntax Question, switching from Perl to Python

Posted on 2006-07-18
10
276 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
  • 6
  • 3
10 Comments
 
LVL 28

Accepted Solution

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

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 28

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 28

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 28

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 28

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

808 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