?
Solved

Basic Syntax Question, switching from Perl to Python

Posted on 2006-07-18
10
Medium Priority
?
305 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
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…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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 …
Suggested Courses

762 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