Created
November 29, 2017 07:17
-
-
Save Everfighting/b5b36890018f4b6278057781500b0d14 to your computer and use it in GitHub Desktop.
在数据库内部查询大批量数据,条件uid来自多超多行csv文件。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # encoding: utf-8 | |
| import pandas as pd | |
| import psycopg2 | |
| conn_dv = psycopg2.connect(database="", user="", password="") | |
| def read_csv(filename): | |
| usr_list = pd.read_csv(filename) | |
| res = [] | |
| for i in usr_list.values: | |
| res.extend(i) | |
| return tuple(res) | |
| def sql_to_csv(sql, conn): | |
| df = pd.read_sql_query(sql, conn) | |
| df.to_csv('tmp.csv', index=False) | |
| return df | |
| if __name__=='__main__': | |
| usr_ids = read_csv('name.csv') | |
| sql = """select * from usr_info where cust_id = {uid};""".format(uid=usr_ids) | |
| df = sql_to_csv(sql, conn_dv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment