Storing user data in a Python script

前端 未结 4 1692
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 13:33

What is the preferred/ usual way of storing data that is entered by the user when running a Python script, if I need the data again the next time the script runs?

Fo

相关标签:
4条回答
  • 2020-12-15 13:57

    For 100 lines, plain text is fine with either the standard ConfigParser or csv modules.

    Assuming your data structure is simple, text affords opportunities (e.g. grep, vi, notepad) that more complex formats preclude.

    0 讨论(0)
  • 2020-12-15 13:59

    For small amounts of data, Python's pickle module is great for stashing away data you want easy access to later--just pickle the data objects from memory and write to a (hidden) file in the user's home folder (good for Linux etc.) or Application Data (on Windows).

    Of, as @aaronnasterling mentioned, a sqlite3 file-based database is small, fast and easy that it's no wonder that so many popular programs like Firefox and Pidgin use it.

    0 讨论(0)
  • 2020-12-15 14:13

    you could use a slite database or a CSV file. They are both very easy to work with but lend themselves to rows with the same type of information.

    The best option might be shelve module

    import shelve
    
    shelf = shelve.open(filename)
    shelf['key1'] = value1
    shelf['key2'] = value2
    
    shelf.close()
     # next run
    shelf.open(filename)
    
    value1 = shelf['key1']
    #etc
    
    0 讨论(0)
  • 2020-12-15 14:19

    Since you only need the last result, just store the result in a file.

    Example

    write('something', wb)
    

    It will only store the last result. Then when you re-run the script, do a open and read the previous result.

    0 讨论(0)
提交回复
热议问题