Results 1 to 7 of 7

Thread: saving integers to text file in python

  1. #1
    Join Date
    Dec 2008
    Beans
    92

    saving integers to text file in python

    I wonder if anyone can help. I am trying to add a value("week") to a text file everytime I run a program. I have converted the number to a string but want to convert all the accumulated data back to integers to perform calculations on them. I have been struggling for ages with this. Someone tried to help me with pickling the information, but for all their help I could only return the first item in the pickled list - so came back to writing to a text file !!!!!

    I get an error message when trying to convert the strings back to numbers.





    Code:
     def add_weekly(week):
                week1 = str(week)
                    
                filename = "turnover.dat"
                FILE = open(filename,"a")
                
                FILE.write ("\n" + week1)
    
                FILE.close()
                filename = "turnover.dat"
                FILE = open(filename,"r")
                lines = FILE.readlines()
    
                j = []
                for line in lines:
                    j.append(int(line))
    
                print(j)

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: saving integers to text file in python

    FILE.write ("\n" + week1)
    here you possibly write a empty line in the beginning of the file which is not handled when you read it back in.

  3. #3
    Join Date
    Feb 2010
    Location
    Silicon Valley
    Beans
    1,898
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: saving integers to text file in python

    Newlines belong at the end of the string, not the beginning.
    Code:
    FILE.write (week1 + "\n")

  4. #4
    Join Date
    Dec 2008
    Beans
    92

    Re: saving integers to text file in python

    Unfortunately, I still get an error even when putting the newline afterwards:
    Code:
            def add_weekly(week):
                week1 = str(week)
                    
                filename = "turnover.dat"
                FILE = open(filename,"a")
                
                FILE.write (week1 + "\n")
    
                FILE.close()
                filename = "turnover.dat"
                FILE = open(filename,"r")
                lines = FILE.readlines()
    
                j = []
                for line in lines:
                    j.append(int(line))
    
                print(j)

  5. #5
    Join Date
    May 2006
    Beans
    1,790

    Re: saving integers to text file in python

    Quote Originally Posted by Bigmon View Post
    Unfortunately, I still get an error even when putting the newline afterwards:
    Code:
            def add_weekly(week):
                week1 = str(week)
                    
                filename = "turnover.dat"
                FILE = open(filename,"a")
                
                FILE.write (week1 + "\n")
    
                FILE.close()
                filename = "turnover.dat"
                FILE = open(filename,"r")
                lines = FILE.readlines()
    
                j = []
                for line in lines:
                    j.append(int(line))
    
                print(j)
    Maybe your data file still contains data in the wrong format? It works for me. What is the error message?

  6. #6
    Join Date
    Dec 2008
    Beans
    92

    Thumbs up Re: saving integers to text file in python

    Thanks. That was it. I deleted the data file, and once a new one was created it worked fine.

  7. #7
    Join Date
    Apr 2006
    Beans
    95

    Re: saving integers to text file in python

    I made two basic changes; the first was to make it easy to test, and the second was to get rid of the word 'FILE' as a file indicator.

    It works fine. You should be easily able to change it to a format of your liking just don't use 'FILE' as a file indicator.

    #!/usr/bin/env python
    #tstweekly.py

    def add_weekly(week):
    week1 = str(week)

    filename = "turnover.dat"
    f1= open(filename,"a")

    f1.write (week1 + "\n")

    f1.close()

    def printdata():
    filename = "turnover.dat"
    f1 = open(filename,"r")
    lines = f1.readlines()
    j = []
    for line in lines:
    j.append(int(line))
    print(j)


    def main():
    wklst=[10,20,30,40,50,60,70,80,90,100]
    for item in wklst:
    add_weekly(item)
    printdata()
    return 0

    if __name__ == '__main__':
    main()

    Thorn

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •