using split function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amitsoni.1984@gmail.com

    #1

    using split function

    Hi,

    I have to write a code in python to read a matrix from a text file and
    for that i am using following code. But it gives an error saying
    "NameError: name 'split' is not defined". Can anyone help me with this.
    -------------------------------------------------
    #!/usr/bin/python
    import numpy
    file = open('matrix.tx t', 'r')

    count = 0
    ra = numpy.random
    A = ra.standard_nor mal((4,4))
    while 1:
    lineStr = file.readline()
    if not(lineStr):
    break

    count = count + 1
    row=split(lineS tr)
    A[count,:]=row

    matrix.close()
    -----------------------------------------------------
    Also, i want to initialize the matrix A by zeros, but using A=zeros([4,
    4]) was giving a similar error "NameError: name 'zeros' is not
    defined".

    Thank you
    Amit

  • ina

    #2
    Re: using split function


    amitsoni.1984@g mail.com wrote:
    Hi,
    >
    I have to write a code in python to read a matrix from a text file and
    for that i am using following code. But it gives an error saying
    "NameError: name 'split' is not defined". Can anyone help me with this.
    -------------------------------------------------
    #!/usr/bin/python
    import numpy
    file = open('matrix.tx t', 'r')
    >
    count = 0
    ra = numpy.random
    A = ra.standard_nor mal((4,4))
    while 1:
    lineStr = file.readline()
    if not(lineStr):
    break
    >
    count = count + 1
    row=split(lineS tr)
    A[count,:]=row
    >
    matrix.close()
    -----------------------------------------------------
    Also, i want to initialize the matrix A by zeros, but using A=zeros([4,
    4]) was giving a similar error "NameError: name 'zeros' is not
    defined".
    >
    Thank you
    Amit
    This is how I would do it.
    for lineStr in file:
    ....row = lineStr.split()

    you could also use str.split(lineS tr) but the other way is cleaner

    Comment

    • Gabriel Genellina

      #3
      Re: using split function

      I have to write a code in python to read a matrix from a text file and
      for that i am using following code. But it gives an error saying
      "NameError: name 'split' is not defined". Can anyone help me with this.
      A few hints:
      - don't use "file" as a name - it shadows the builtin "file" type
      - matrix.close() won't work, perhaps you meant file.close()?
      -----------------------------------------------------
      Also, i want to initialize the matrix A by zeros, but using A=zeros([4,
      4]) was giving a similar error "NameError: name 'zeros' is not
      defined".
      Oh, so *that's* why you build it using standard_normal and then
      overwrite the contents!


      --
      Gabriel Genellina
      Softlab SRL

      _______________ _______________ _______________ _____
      Correo Yahoo!
      Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
      ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

      Comment

      • amitsoni.1984@gmail.com

        #4
        Re: using split function

        Thanks a lot, I am done with that part. But now I am facing another
        problem. I am using the code given below where A is a matrix and row is
        a sequence. But it gives following error:

        -------- error------
        A[a,:]=row
        ValueError: setting an array element with a sequence.

        --------------code----------------
        #!/usr/bin/python
        import numpy
        file1 = open('matrix.tx t', 'r')

        count = 0
        a=0
        b=0
        c=0
        d=0
        e=0
        A = numpy.zeros([4,4])
        while 1:
        lineStr = file1.readline( )
        if not(lineStr):
        break

        count = count + 1
        row=lineStr.spl it()
        if count<=4:
        A[a,:]=row
        a=a+1
        elif count<=8:
        B[b,:]=row
        b=b+1
        elif count<=12:
        C[c,:]=row
        c=c+1
        elif count<=16:
        D[d,:]=row
        d=d+1
        elif count<=20:
        E[e,:]=row
        e=e+1

        file1.close()
        ---------end of code-------------

        is there any way to change a sequence to array?
        thank you
        Amit

        Gabriel Genellina wrote:
        I have to write a code in python to read a matrix from a text file and
        for that i am using following code. But it gives an error saying
        "NameError: name 'split' is not defined". Can anyone help me with this.
        >
        A few hints:
        - don't use "file" as a name - it shadows the builtin "file" type
        - matrix.close() won't work, perhaps you meant file.close()?
        >
        -----------------------------------------------------
        Also, i want to initialize the matrix A by zeros, but using A=zeros([4,
        4]) was giving a similar error "NameError: name 'zeros' is not
        defined".
        >
        Oh, so *that's* why you build it using standard_normal and then
        overwrite the contents!
        >
        >
        --
        Gabriel Genellina
        Softlab SRL
        >
        _______________ _______________ _______________ _____
        Correo Yahoo!
        Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
        ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar

        Comment

        Working...