好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java 直接调用python脚本,并传递参数代码实例

最近陆续用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测试数据mit()

     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脚本传递参数详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

查看更多关于java 直接调用python脚本,并传递参数代码实例的详细内容...

  阅读:27次