Solved

Basic Syntax Question, switching from Perl to Python

Posted on 2006-07-18
10
259 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
Comment Utility
tabDelimitedFields = 'data1\tdata2\tdata3'
field1, field2, field3 = tabDelimitedFields.split('\t')
print field1
print field2
print field3
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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
Comment Utility
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
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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
Comment Utility
pepr: Did you mean re.split()?

>>> import re
>>> re.split('\t', 'data1\tdata2\tdata3')
['data1', 'data2', 'data3']
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 28

Expert Comment

by:pepr
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks! I'm now well on my way to leaving Perl for Python.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
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 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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now