Web Analytics Made Easy -
StatCounter Print formatting and loading a string with a line from a text file - CodingForum

Announcement

Collapse
No announcement yet.

Print formatting and loading a string with a line from a text file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Print formatting and loading a string with a line from a text file

    Hi,

    I'm very new to Python (using version 2.7)

    How do you print a numerical variable, say:

    "Percentage of households with cats: 0.54333201"

    Also how do I load a line from a file into a string variable.

    Say: String$ = line.

  • #2
    PRINT

    cats_p = 0.543333

    print "percentage of households with cats {0}".format(cats_p)

    LINE FROM FILE

    with open(filename) as f:
    l = f.readline()


    Note that everytime you run readline(), it'll go to the next line. This is generator behavior. You have load the entire contents into a variable by using read().


    Sent from my iPhone using Tapatalk

    Comment


    • #3
      The following code will solve your problem statement :

      Code:
      cats=0.54333201
      print("Percentage of households with cats: ",float(cats))
      file=open('C:\\Users\\LENOVO\\Desktop\\form1.txt', 'r')
      data = file.readline()
      print(data)
      file.readline() will read next line everytime. If you want to read the whole text from the file, then you can use readlines() and for reading words you can can use read() function or method.
      ASP.Net Software Development

      Comment

      Working...
      X