How to use a Python script to query the value of a remote database and insert that value into column B as a whole in the CSV "column A" relationship, recently handle a case, Associating python with the obvious advantage of data processing over the shell, the final attempt to script this seemingly simple, but not simple, data processing.
Target:
Column A is a number of project name stored in DB, and you need to query the database for the specific values of the project name in the DB, and insert column A to save.
#!/usr/bin/env python # encoding: utf-8 import mysqldb import csv import sys # Define csv list csv_content = [] csv_content_edited = [] # Define project list project_names = [] # define db list db_content = [] # Define
the file that needs to be handled. Try: file_name = sys.argv[1] new_file_name = file_name.split ('. ') [0] + ' _new. ' + file_name.split ('. ') [1] Except indexerror: pass def selectdb (): # open db connection db = mysqldb.connect ("test.com", "TestUser", "TestUser", "TestDB") # use cursor () fuction to get current db cursor cursor = Db.cursor () csv_content_edited = readcontent (file_name) for c in range (1, Len (Csv_content_edited)): project_names.append (csv_content_edited[c][0]) project_name_string = ",". Join ([' "' + p + '" ' for p in project_names]) # sql "SELECT" statement sql = ' Select pname,pkey from project where
pname in (%s) ' %project_name_string try: # execute sql cursor.execute (SQL) # obtain all the record list Results = cursor.fetchall () for row in results: #lower_ USER_NAME = ROW[3] db_content.append (row) return db_content Except: print "Error: unable to fecth data" # Close Connection db.close () def readcontent (file_name): # read the csv file,then put&nbsP;it into list. with open (file_name, ' R ') as csvfile: csv_reader = csv.reader ( Csvfile, delimiter= ', ') for row in csv_reader: if row[0]: csv_content.append (row) return csv_content def insert_col (): csv_content_edited = readcontent (file_name) # insert null
value to each components of "csv_content_edited" afterward.
for i in range (0,len (csv_content_edited)): csv_content_edited[i].insert (1, "")
# define the second inserted column title. Csv_content_edited[0][1] = "Pkey" # grab the users data from
db. db_content = selectdb () # print db_content for d in Range (0,len (db_contenT)): pkey = db_content[d][1] pname = db_content[d][0] for c in range (0,len (csv_content_edited)): if csv_content_edited C [0] == pname: csv_content_edited[c][1] = pkey # print csv_content_edited pname_list = [] csv_content_edited_new = [] For c in range (0,len (csv_content_edited)): if not csv_content_edited[c][0] in pname_list: pname_list.append (csv_content_edited[c][0]) Csv_ Content_edited_new.append (Csv_content_edited[c]) # print csv_content_edited_new Return csv_content_edited_new # write the csv file. def writeContent () : with open (New_file_name, ' WB ') as csvfile: csv_writer = csv.writER (csvfile) csv_writer.writerows (Insert_col ()) # execute the finnal function. if __name__ == ' __main__ ': try: writecontent () except (IOError, Nameerror,indexerror): print "Please type the correct file name." e.g: ' " + sys.argv[0] + " testfile.csv ' " else: print ' the result file is: %s ' %new_file_name
Result: