[Tutor] Re: importing into fields

Lee Harr missive at hotmail.com
Mon Jan 5 17:20:42 EST 2004


>I'm trying to manipulate a string to seperate before entering each seperate 
>instance in >some fields on a software program, here's an example of the 
>script
>
>f=open ("book2.txt", "r")
>s=f.readline ()
>while s != "":
>  print s
>  l = string.split(s, ",",[11])
>


You use the string module here. If you want to do that, you will
have to put

import string

before that in the file. The alternative (and recommended) way
would be to use "string methods" which would look like ...

s.split(",")

I am also not sure about your string.split call ... You probably want
to look at the docs to be sure you are calling properly.  If you use

string.split(s, ",", 11)

that would split string s at commas returning at most 12 pieces.

>>>a='1, 2, 3, 4'
>>>import string
>>>string.split(a, ',', 2)
['1', ' 2', ' 3, 4']
>>>a.split(',', 2)
['1', ' 2', ' 3, 4']
>>>a.split(',')
['1', ' 2', ' 3', ' 4']

_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* 
http://join.msn.com/?page=features/junkmail




More information about the Tutor mailing list