Jython

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Boris Ozegovic

    #1

    Jython

    Hi

    Why this doesn't work:

    def go():
    for line in open("bobo.txt" , "r"):
    print line

    go()

    python FileReader.py: everything ok
    jython FileReader.py:

    Traceback (innermost last):
    File "FileReader.py" , line 6
    File "FileReader.py" , line 3
    AttributeError: __getitem__

    --
    "A mi smo stranci u vlastitoj zemlji zbog ljudskog sljama, lipa nasa
    silovana"
  • gregturn@mindspring.com

    #2
    Re: Jython

    On Feb 3, 11:21 am, Boris Ozegovic <silovana.vjeve r...@com.gmail>
    wrote:
    Hi
    >
    Why this doesn't work:
    >
    def go():
    for line in open("bobo.txt" , "r"):
    print line
    >
    go()
    >
    python FileReader.py: everything ok
    jython FileReader.py:
    >
    Traceback (innermost last):
    File "FileReader.py" , line 6
    File "FileReader.py" , line 3
    AttributeError: __getitem__
    >
    --
    "A mi smo stranci u vlastitoj zemlji zbog ljudskog sljama, lipa nasa
    silovana"
    Files aren't lists and thus don't have the functions for iteration.

    Try:

    def go():
    for line in open("bobo.txt" , "r").readlines( ):
    print line

    go()

    Comment

    • Boris Ozegovic

      #3
      Re: Jython

      gregturn@mindsp ring.com wrote:
      Files aren't lists and thus don't have the functions for iteration.
      They do have iterator:

      C:\Documents and Settings\Silova na Vjeverica\Deskt op>python FileReader.py
      'import site' failed; use -v for traceback
      boris ozegovic
      vedran ozegovic

      --
      "A mi smo stranci u vlastitoj zemlji zbog ljudskog sljama, lipa nasa
      silovana"

      Comment

      • Boris Ozegovic

        #4
        Re: Jython

        Boris Ozegovic wrote:
        gregturn@mindsp ring.com wrote:
        >
        >Files aren't lists and thus don't have the functions for iteration.
        >
        They do have iterator:
        >
        C:\Documents and Settings\Silova na Vjeverica\Deskt op>python FileReader.py
        'import site' failed; use -v for traceback
        boris ozegovic
        vedran ozegovic
        But apparently not in Jython... Only Python.

        Tnx, anyway

        --
        "A mi smo stranci u vlastitoj zemlji zbog ljudskog sljama, lipa nasa
        silovana"

        Comment

        • Boris Ozegovic

          #5
          Re: Jython

          gregturn@mindsp ring.com wrote:
          Files aren't lists and thus don't have the functions for iteration.
          >
          Try:
          >
          def go():
          for line in open("bobo.txt" , "r").readlines( ):
          print line
          >
          go()
          For example, you can do even this:

          import sys

          for line in sys.stdin:
          print line,


          python FileReader.py < bobo.tx

          'import site' failed; use -v for traceback
          boris ozegovic
          vedran ozegovic

          --
          "A mi smo stranci u vlastitoj zemlji zbog ljudskog sljama, lipa nasa
          silovana"

          Comment

          • Peter Otten

            #6
            Re: Jython

            Boris Ozegovic wrote:
            Boris Ozegovic wrote:
            >
            >gregturn@mindsp ring.com wrote:
            >>
            >>Files aren't lists and thus don't have the functions for iteration.
            >>
            >They do have iterator:
            >>
            >C:\Documents and Settings\Silova na Vjeverica\Deskt op>python FileReader.py
            >'import site' failed; use -v for traceback
            >boris ozegovic
            >vedran ozegovic
            >
            But apparently not in Jython... Only Python.
            Iterable files were introduced in Python 2.2. Jython implements Python2.1,
            it seems.

            Peter

            Comment

            • bearophileHUGS@lycos.com

              #7
              Re: Jython

              gregt...@mindsp ring.com:
              Files aren't lists and thus don't have the functions for iteration.
              Try:
              def go():
              for line in open("bobo.txt" , "r").readlines( ):
              print line
              go()
              CPython 2.1 has xreadlines, maybe Jython has it too.

              Bye,
              bearophile

              Comment

              Working...