how to read to and write from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mar12
    New Member
    • Dec 2013
    • 9

    how to read to and write from a file

    Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?
  • mar12
    New Member
    • Dec 2013
    • 9

    #2
    How do i initialise the r and w command?

    Comment

    • Luuk
      Recognized Expert Top Contributor
      • Mar 2012
      • 1043

      #3
      i started reading here:



      and came up with:
      Code:
      luuk@opensuse:~/temp> cat abcd
      a
      b
      c
      d
      
      luuk@opensuse:~/temp> cat abcd.py
      #!/usr/bin/python
      
      f = open('abcd','r')
      for line in f:
        print ord(line[0:1])
      
      luuk@opensuse:~/temp> ./abcd.py
      97
      98
      99
      100
      10
      luuk@opensuse:~/temp>

      Comment

      • Luuk
        Recognized Expert Top Contributor
        • Mar 2012
        • 1043

        #4
        but slightly better:
        Code:
        #!/usr/bin/python
        
        f = open('abcd','r')
        c = True
        while c:
                c = f.read(1)
                if c>' ':
                        print ord(c)

        Comment

        • mar12
          New Member
          • Dec 2013
          • 9

          #5
          Hi thanks, i created a text file with the same characters as my input but the code "print ord(line[0:1])" is not working. I know ord is for ascii.

          Comment

          • mar12
            New Member
            • Dec 2013
            • 9

            #6
            Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?

            Comment

            • Luuk
              Recognized Expert Top Contributor
              • Mar 2012
              • 1043

              #7
              If your input file contains this:
              Code:
              1 34
              12 4
              123
              can you tell, what the expected output should be?

              Because you are talking about 'product', and as far as my English goes, the product of 2 and 3 is (2x3=) 6
              But i only see additions in your 'code-so far'...

              Comment

              • mar12
                New Member
                • Dec 2013
                • 9

                #8
                Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?

                Comment

                • Luuk
                  Recognized Expert Top Contributor
                  • Mar 2012
                  • 1043

                  #9
                  I asked a question, and i did not get an answer.....

                  Why do you think i asked that question?

                  Just so you could post your code again, without specifying what kind of error you have? and on what line the error is?

                  rule1: please use '[CODE]' tags around your code
                  rule2: only post code if its different from previous post ;)
                  rule3: answer a question when asked, how else can you be helped?
                  rule4: i do not make rules here........

                  But, to answer your question,.....

                  Your code gives an error, on what line, and what error?
                  Code:
                      wordlist = line.split()
                             ^
                  IndentationError: expected an indented block
                  It it's not this error, than please use '[CODE]' tags to post your code..... ;)

                  Comment

                  • mar12
                    New Member
                    • Dec 2013
                    • 9

                    #10
                    not outputting the result in another file named b.txt

                    Comment

                    • mar12
                      New Member
                      • Dec 2013
                      • 9

                      #11
                      not outputting the result in b.txt

                      Comment

                      • Luuk
                        Recognized Expert Top Contributor
                        • Mar 2012
                        • 1043

                        #12
                        maybe because you are only outputting it to screen:
                        Code:
                        print("The product is", product)

                        Comment

                        • mar12
                          New Member
                          • Dec 2013
                          • 9

                          #13
                          Here is the problem:
                          y.write(line) outputs the result as characters in b.txt
                          but i need the result as integer value.

                          Comment

                          • Luuk
                            Recognized Expert Top Contributor
                            • Mar 2012
                            • 1043

                            #14
                            "y.write(li ne) outputs the result as characters in b.txt"
                            what is y ??

                            Comment

                            Working...