- Joined
- Mar 30, 2005
- Messages
- 2,205
If remember correctly perl has a function called split that will take a string and break it up in to several strings and put them into an array. Is there any built in function like this in python?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
python.org said:split( [sep [,maxsplit]])
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified, then there is no limit on the number of splits (all possible splits are made). Consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, "'1,,2'.split(',')"returns "['1', '', '2']"). The sep argument may consist of multiple characters (for example, "'1, 2, 3'.split(', ')" returns "['1', '2', '3']"). Splitting an empty string with a specified separator returns "['']".
If sep is not specified or is None, a different splitting algorithm is applied. First, whitespace characters (spaces, tabs, newlines, returns, and formfeeds) are stripped from both ends. Then, words are separated by arbitrary length strings of whitespace characters. Consecutive whitespace delimiters are treated as a single delimiter ("'1 2 3'.split()" returns "['1', '2', '3']"). Splitting an empty string or a string consisting of just whitespace returns an empty list.
infile = open( 'memwarehouse.dat', 'r' )
print "<table>"
print "<tr>"
print "<th>Part Number</th>"
print "<th>Part Description</th>"
print "<th>Numberin Stock</th>"
print "<th>Price per Item</th>"
print "</tr>"
for line in infile:
data = split( " ", line )
print "<tr>"
for g in data:
print "<td>" + g + "</td>"
print "</td>"
HHunt said:Try data = line.split() instead.
(Split() is a method of string objects. Very OO.)
>>> a,b = "1", 2
>>> a, b
('1', 2)
>>> type(a), type(b)
(<type 'str'>, <type 'int'>)
>>> a+b
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: cannot concatenate 'str' and 'int' objects
>>> int(a) + b
3
>>> a + str(b)
'12'
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
metallicafan said:HHunt - thank you very much. I am somewhat familiar with OO programming. Your explanation help clear things up somewhat. I am just new to Python and the syntax takes some getting used to when I am used to C++. Thanks again.
HHunt said:Ah, right. At least I didn't underexplain it.![]()