Text doc lists [solved]

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bellum
    New Member
    • Sep 2006
    • 23

    Text doc lists [solved]

    Hi, I'm trying to use a .txt file to store data (in lists) for python, but I've got no idea how to go about this. Would that even be a good way to store lists?
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by Bellum
    Hi, I'm trying to use a .txt file to store data (in lists) for python, but I've got no idea how to go about this. Would that even be a good way to store lists?
    If you've got lists of strings, it's really easy...
    Code:
    filename = r"C:\myFile"   # (r)aw strings work for windows dir names
    outputFile = file(fileName, "w")	# open/create a file object in (w)rite mode
    outputFile.writelines(stringList)
    outputFile.close()

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Following is what I have been doing to save dictionaries. It should work for lists also.
      Code:
      import pickle
      def export_data(file_name, dd):
          try:
              f = open(file_name, "w")
              pickle.Pickler(f).dump(dd)
              f.close()
              return dd
          except:
              Warning("Default file export error.")
              return 0
      
      export_data('your_file_name.txt', your_list)
      You can use Unpickler to read your list from disk.

      Comment

      • Bellum
        New Member
        • Sep 2006
        • 23

        #4
        Hey, thanks you guys.

        Comment

        Working...