[Tutor] importing into fields

Karl Pflästerer sigurd at 12move.de
Mon Jan 5 18:31:25 EST 2004


On  3 Jan 2004, Chris Rosenboom <- boomer4467 at yahoo.com wrote:

> 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 never imported string; so you have the name error.
Furthermore the [11] is alist; but you need to give a integer as
argument here.

>   PlayIt.SetFieldContent ("SY012M1", "art-nr", l[0])
[...]
>   s=f.readline ()
> f.close ()

> this is the error returned:

> Traceback (innermost last):
>   File "<string>", line 5, in ?
> NameError: string

But there is no need to import string.


> I've also tried this in line 5

> l = (s, ",",[11])

> I get further with this I actually get the first part of the string in the
> first field like it should be but in the second field all i get is a comma and
> it freezes, Initially I've been trying to import this from a csv file and now
> I've been working with text files. I'm sure I just don't know how to
> manipulate the string and I'm having trouble finding descriptions and methods
> to use with a string object. Do you have any suggestions or anything that
> would be helpful? Thank You,


First there should be a lot of documentation coming with your Python
system.  Then a lot of docu can be read interactively. Eg. if you wanted
to know which methods strings have you just type:

>>> s = ""
>>> dir(s)
['__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', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> 

The first line created a string, the second line shows me all its
methods.  The same works for every object.  Also try help()

>>> help()

Welcome to Python 2.3!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://www.python.org/doc/tut/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> 


You can read a lot there.

But back to your problem.

I assume you have a relatively current version of Python so your code
can be written a bit simpler and IMO easier to read.

f=open ("book2.txt")
for line in f:
  print line
  L = line.split(",", 11)
  PlayIt.SetFieldContent ("SY012M1", "art-nr", L[0])
  PlayIt.PlayContent ("{CSB SY012M1|art-nr}{Enter}")
  PlayIt.SetFieldContent ("SY012ADM1", "001bez",  L[1])
  PlayIt.SetFieldContent ("SY012ADM1", "005agr",  L[2])
  PlayIt.SetFieldContent ("SY012ADM1", "006agr",  L[3])
  PlayIt.SetFieldContent ("SY012ADM1", "009kst",  L[4])
  PlayIt.SetFieldContent ("SY012EHM1", "005laeh", L[5])
  PlayIt.SetFieldContent ("SY012EHM1", "006lauf", L[6])
  PlayIt.SetFieldContent ("SY012EHM1", "011vkuf", L[7])
  PlayIt.SetFieldContent ("SY012SDM1", "012fest", L[8])
  PlayIt.SetFieldContent ("SY012PRM1", "001tpr",  L[9])
  PlayIt.SetFieldContent ("SY012PRM1", "002wpr",  L[10])
  PlayIt.SetFieldContent ("SY012PRM1", "003plpr", L[11])
  PlayIt.PlayContent ("{CSB SY012M1|art-nr}{F2}")

f.close()

(I replaced your l with L since IMO a single smallcaps l is hard to
distinguish from a 1.)



   Karl
-- 
Please do *not* send copies of replies to me.
I read the list




More information about the Tutor mailing list