最近陸續用python寫了很多文件處理腳本,雖然功能都比較簡單 ,但還是感覺到python對文件處理的簡潔高效 ,越發覺得java的語法相當的繁瑣~
接到個需求處理ftp數據接口 。所以想把python腳本也用上。java代碼定時掃描ftp數據倉庫 ,調用python腳本入庫。
直接采用java執行系統命令的方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
@async public void readfilebypython(list<string> filepaths) 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<filepaths.size() ;i++){ int index = i+ 2 ; args[index] = filepaths.get(i); } try { system.out.println( "start" ); process proc = runtime.getruntime().exec(args); inputstream is = proc.geterrorstream(); inputstreamreader isr = new inputstreamreader(is); bufferedreader br = new bufferedreader(isr); string line = null ; system.out.println( "<error>" ); while ((line = br.readline())!= null ){ system.out.println(line); system.out.println( "</error>" ); 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]表示要執行的是python 腳本 ,args[1] 腳本文件的全路徑
該方法調用 abstractreadfileline.class 文件路徑下的 pythonfilehandle.py 腳本 ,并傳入string數組類型的參數(需要處理的文件全路徑)
pythonfilehandle腳本接受java傳入的文件路徑參數(數組),解析并入庫
pythonfilehandle.py 代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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] 存儲的是py文件自身的路徑,故接受參數從sys.argv[1]開始。
以上所述是小編給大家介紹的java調用python腳本傳遞參數詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!