最近陸續(xù)用python寫了很多文件處理腳本,雖然功能都比較簡(jiǎn)單 ,但還是感覺到python對(duì)文件處理的簡(jiǎn)潔高效 ,越發(fā)覺得java的語法相當(dāng)?shù)姆爆崀
創(chuàng)新互聯(lián)專注于榆陽網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供榆陽營(yíng)銷型網(wǎng)站建設(shè),榆陽網(wǎng)站制作、榆陽網(wǎng)頁設(shè)計(jì)、榆陽網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造榆陽網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供榆陽網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
接到個(gè)需求處理ftp數(shù)據(jù)接口 。所以想把python腳本也用上。java代碼定時(shí)掃描ftp數(shù)據(jù)倉庫 ,調(diào)用python腳本入庫。
直接采用java執(zhí)行系統(tǒng)命令的方式
@Async public void readFileByPython(ListfilePaths) throws FileNotFoundException { URL localSrcUrl = AbstractReadFileLine.class.getResource(""); String localSrcPath = localSrcUrl.getPath(); localSrcPath = localSrcPath.substring(1, localSrcPath.length()); String pythonFile = localSrcPath + "PythonFileHandle.py"; int size = filePaths.size() + 2; String[] args = new String[size]; args[0] = "python"; args[1] = pythonFile; for(int i =0;i "); while((line = br.readLine())!=null){ System.out.println(line); System.out.println(""); int exitValue = proc.waitFor(); System.out.println("Process exitValue="+exitValue); } System.out.println("end"); } catch (Exception e){ e.printStackTrace(); } }
String[] args = new String[size];
args[0] = "python"; args[1] = pythonFile; args[0]表示要執(zhí)行的是python 腳本 ,args[1] 腳本文件的全路徑
該方法調(diào)用 AbstractReadFileLine.class 文件路徑下的 PythonFileHandle.py 腳本 ,并傳入String數(shù)組類型的參數(shù)(需要處理的文件全路徑)
PythonFileHandle腳本接受java傳入的文件路徑參數(shù)(數(shù)組),解析并入庫
PythonFileHandle.py 代碼
import pymssql,time,sys reload(sys) sys.setdefaultencoding("utf-8") class MSSQL: def __init__(self,host,user,pwd,db): self.host = host self.user = user self.pwd = pwd self.db = db def __GetConnect(self): if not self.db: raise(NameError,"") self.conn = pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset="utf8") cur = self.conn.cursor() if not cur: raise(NameError,"") else: return cur def ExecQuery(self,sql): cur = self.__GetConnect() cur.execute(sql) resList = cur.fetchall() # self.conn.close() return resList def ExecNonQuery(self,sql): cur = self.__GetConnect() cur.execute(sql) self.conn.commit() self.conn.close() def insertToCloseList(data ,ms): sql = "insert into t_isee_closelist_infos (WORK_ORDER_ID,CRM_Cdsc_Id,APPRAISE_TYPE,CRM_Accept_Date,latn_code,theme_row_id,TASK_EXECUTE_ROW_ID,CRM_Accept_REASON,ASSET_INTEG_ID) values ( '" temp ="' , '" sqlStr = temp.join(data) sql = sql + sqlStr + "')" ms.ExecNonQuery(sql) ms = MSSQL(host="172.30.0.186",user="sa",pwd="",db="test") fengefu = '$%$' for i in range(1, len(sys.argv)): read = open(sys.argv[i] ,'r') for line in read: line=line.strip('\n') data = line.split(fengefu) insertToCloseList(data,ms) read.close
sys.argv[0] 存儲(chǔ)的是py文件自身的路徑,故接受參數(shù)從sys.argv[1]開始。
以上所述是小編給大家介紹的java調(diào)用python腳本傳遞參數(shù)詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!