一、环境:
- #include "stdafx.h"
- #include <Windows.h>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- char* wait = "" ;
- STARTUPINFO si = { sizeof(si) };
- PROCESS_INFORMATION pi;
- TCHAR szCommandLine[] = TEXT( "F:\\backup\\4\\dist\\writeCSV.exe --p F:\\wordpart.txt" );
- TCHAR szCmd[16] = {0};
- if(!CreateProcess(NULL, szCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
- {
- cout<< "fail!"<<GetLastError()<<endl;
- };
- WaitForSingleObject(pi.hProcess,INFINITE);
- cout<< "finished!";
- cin>>wait;
- return 0;
- }
- #coding:utf-8
- import os, sys
- import MySQLdb
- def parseCSV(outPath):
- '''''parse the .csv/.out file'''
- wordGroups = []#return the [[word,time,frequency]...]
- outFile = open(outPath,'r')
- allLines = outFile.readlines()
- for eachline in allLines:
- lineParts = eachline.split(',')
- if len(lineParts) != 3:
- print 'wrong line format!'
- else:
- group = lineParts#[word, time, frequency]
- wordGroups.append(group)
- outFile.close()
- return wordGroups
- def writeToDB(wordGroups):
- conn = MySQLdb.connect(host = "localhost",user="root",\
- passwd = '111111',db='microblog',use_unicode=1\
- charset='utf8')
- cursor = conn.cursor()
- sql = "select word from word_frequency"
- count = cursor.execute(sql)
- rows = cursor.fetchall()
- wordAlreadyExists = rows[0:]["word"]#all words
- cursor.close()
- cursor = conn.cursor()
- for group in wordGroups:
- try:#in case of duplicated status
- sql = "insert into word_frequency(word,time,frequency) values (%s,%s,%d)"
- param = (group[0],group[1],group[2])
- cursor.execute(sql,param)
- except MySQLdb.Error, e:
- print(e)
- cursor.close()
- conn.commit()
- if __name__ == '__main__':
- '''''Write the c++ output .csv/.out to db
- example: --p [the path of .csv/.out]
- '''
- if len(sys.argv) < 3:
- #no argument, use the default path
- print 'wrong! --p [the path of .csv/.out]'
- elif sys.argv[1].startwith('--'):
- #parse sys.argv[2] execute the write statement
- option = sys.argv[1][2:]
- if option == 'p':
- outPath = sys.argv[2]
- else:
- print 'unknown option'
- # setup.py
- from distutils.core import setup
- import py2exe
- setup(console=["writeCSV.py"])