read line

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

    read line

    hi every body,

    I'm a new python user and I'm making a program to run useing Abaqus
    and there is something I can't do,

    if i have a text file that has a line like this " 10 20 30 40
    50" and I wana do the coding to put every number of these like 10 or
    20 in a separate variable .. any suggestions ??

    thanks in advance
  • Diez B. Roggisch

    #2
    Re: read line

    moustafa.elmihy @gmail.com wrote:
    hi every body,
    >
    I'm a new python user and I'm making a program to run useing Abaqus
    and there is something I can't do,
    >
    if i have a text file that has a line like this " 10 20 30 40
    50" and I wana do the coding to put every number of these like 10 or
    20 in a separate variable .. any suggestions ??
    Look up on the file-objects in the documentation, as well as string-methods
    such as split and the int-function.

    Diez

    Comment

    • Larry Bates

      #3
      Re: read line

      moustafa.elmihy @gmail.com wrote:
      hi every body,
      >
      I'm a new python user and I'm making a program to run useing Abaqus
      and there is something I can't do,
      >
      if i have a text file that has a line like this " 10 20 30 40
      50" and I wana do the coding to put every number of these like 10 or
      20 in a separate variable .. any suggestions ??
      >
      thanks in advance
      The problem with you not actually trying anything and posting it here is that we
      have no real idea if 10, 20, ... 50 should be strings, integers, floats,
      hexadecimal, or decimal "variables" .

      Assuming you mean integers:

      fp=open('textfi le.txt')
      l=fp.readline()
      a,b,c,d,e = [int(x) for x in l.split()]
      fp.close()

      More likely you need:
      values = [int(x) for x in l.split()]

      -Larry

      Comment

      Working...