简体   繁体   English

从文本文件读取查询参数

[英]Reading query parameters from textfile

I want to store the parameters for a mysql-connect in a textfile but do not know, what could be the most elegant way to store them in "query.txt" The readline() command seemed to be an option but somehow makes thing very inelegant. 我想将mysql-connect的参数存储在文本文件中,但不知道,将它们存储在“ query.txt”中的最优雅的方法是什么?readline()命令似乎是一个选项,但某种程度上使事情变得非常重要不雅 Can somebody suggest a smooth solution? 有人可以提出一个平稳的解决方案吗? thanks! 谢谢!

open("query.txt")
??? read parameters host, user, pass, db from query.txt ???
???sql = MySQLdb.connect (host = "%s", user = "%s", passwd = "%s", db = "s%"), host, user, pass, db)‬

This line causes me headache as well. 这条线也让我头疼。 I haven't figured out yet how to correctly make this query.... 我还没有想出如何正确进行此查询。

Yet another way: let's say you have a text file, params.txt , that contains Python-style dictionary literal that has your parameters. 还有另一种方式:假设您有一个文本文件params.txt ,其中包含具有您的参数的Python样式的字典文字。 It might look like this: 它可能看起来像这样:

# my cool SQL parameters
{ "user":   "lilu",
  "passwd": "multipass",
  "host":   "woo.foo.baz",
  "db":     "human-resources" }

You can use a safe and standard Python literals parser ast.literal_eval to convert the contents of this file into a dictionary object, which you can then pass on to your SQL connection function. 您可以使用安全且标准的Python文字解析器ast.literal_eval将此文件的内容转换为字典对象,然后将其传递给SQL连接函数。

import ast

defaults = dict(host='default-host-name', user='default-user-name')

# "U" mode is used so that no matter what newline styles you have in the file,
# they all become \n in memory.
with open('params.txt', 'rU') as params_fo:
    params = dict(defaults)
    params.update(ast.literal_eval(params_fo.read()))
    sql = MySQLdb.connect(**params)

It will better and more confortable for you to use a serialization: 使用序列化会更好,更方便:

import cPickle

with open('pickler file.pic','w') as f:
    host, user, passw, db = 'bouloudou','bididi',198754,'the_db'
    cPickle.dump([host, user, passw, db],f)



with open('pickler file.pic','r') as f:
    host, user, passw, db = cPickle.load(f)

Assuming that "query.txt' contains : 假设“ query.txt”包含:

hostname username password db  

you can do : 你可以做 :

host, user, pass, db = open('query.txt').read().split()
sql = MySQLdb.connect (host = "%s", user = "%s", passwd = "%s", db = "s%"), host, user, pass, db)‬

You can use JSON if you want a text file that's both nicely-formatted and easy to read: 如果您希望文本文件格式正确且易于阅读,则可以使用JSON:

# Query parameters class
class QueryParams:
    def __init__(self, paramsDict):
        self.__dict__.update(paramsDict)

# Read text file
q = json.load(open('query.txt'), object_hook = lambda dc: QueryParams(dc))

# Use params
sql = MySQLdb.connect (host = "%s", user = "%s", passwd = "%s", db = "s%"), q.host, q.user, q.pass, q.db)‬

parameters.txt containing parameters.txt包含

MyHost, MyUsername, MyPassword, MyDb

In the script: 在脚本中:

host, user, passw, db = open('parameters.txt').read().split()
sql = MySQLdb.connect (host , user, passw, db)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM