Solved

Basic Syntax Question, switching from Perl to Python

Posted on 2006-07-18
10
301 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 500 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
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 
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

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.

Question has a verified solution.

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

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

717 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