Java調用python腳本

package com.chrtc.utils; import org.apache.commons.lang3.StringUtils; import org.python.core.*; import org.python.util.PythonInterpreter; import java.io.*; import java.util.Properties; public class FirstJavaScript { public static void main(String args[]) { // exturePy1(); // excure6(); String text= "import pymysql\n" + "import pandas as pd\n" + "conn = pymysql.connect(host='127.0.0.1',user='root',password='root',database='metadata', port=3306, charset='utf8')\n" + "cursor = conn.cursor()\n" + "cursor.execute(\"select * from role\")\n" + "rows = cursor.fetchall()\n" + "data = pd.DataFrame(list(rows))\n" + "print(data)\n" + "data.to_csv(\"D:\\\\cq\\\\python2\\\\text.csv\",encoding=\"utf_8_sig\")"; try { String path = createFile(text); System.out.println(exturePythonScript(path)); } catch (IOException e) { e.printStackTrace(); } /*String result1 =exturePython(); System.out.println(result1);*/ } public static String createFile(String text) throws IOException { String file1 ="D:\\\\cq\\\\python2" + File.separator; File dir = new File(file1); if (!dir.exists()) { dir.mkdirs(); } String filePath = file1+ File.separator + "1" +".py"; File file = new File(filePath); if (!file.exists()) { file.createNewFile(); }else { FileWriter fileWriter =new FileWriter(file); fileWriter.write(""); } FileWriter fileWritter = new FileWriter(file, true); if (StringUtils.isNotBlank(text)) { fileWritter.write(text); } fileWritter.close(); return filePath; } public static String exturePythonScript(String text){ String line = null; String line_all = ""; try { String[] args = new String[] { "python ",text }; Process proc = Runtime.getRuntime().exec(args);// 執行py文件 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GBK")); while ((line=in.readLine()) != null) { line_all = line_all+line; } in.close(); proc.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return line_all; } public static String exturePython(){ String line = null; String line_all = ""; try { String[] args = new String[] { "python", "D:\\cq\\python\\mysql.py"}; Process proc = Runtime.getRuntime().exec(args);// 執行py文件 BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GBK")); while ((line=in.readLine()) != null) { line_all = line_all+line; } in.close(); proc.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return line_all; } private static void exture2(){ PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("a=[5,2,3,9,4,0]; "); interpreter.exec("print(sorted(a));"); //此處python語句是3.x版本的語法 } private static void exturePy3(){ PythonInterpreter interpreter = new PythonInterpreter(); PySystemState sys = Py.getSystemState(); sys.path.add("C:\\Users\\104005145\\Anaconda3\\Lib\\site-packages"); interpreter.exec("import pymysql"); interpreter.exec("print pymysql.path"); interpreter.exec("path = \"C:\\Users\\104005145\\Anaconda3\\Lib\\site-packages\""); interpreter.exec("pymysql.path.append(path)"); interpreter.exec("print pymysql.path"); InputStream filepy = null; try { filepy = new FileInputStream("D:\\cq\\python\\mysql.py"); interpreter.execfile(filepy); filepy.close(); } catch (Exception e) { e.printStackTrace(); } } private static void exture4(){ Properties props = new Properties(); props.put("python.home", "C:\\Users\\104005145\\Anaconda3"); props.put("python.console.encoding", "UTF-8"); props.put("python.security.respectJavaAccessibility", "false"); props.put("python.import.site", "false"); Properties preprops = System.getProperties(); PythonInterpreter.initialize(preprops, props, new String[0]); PythonInterpreter pyInterpreter = new PythonInterpreter(); pyInterpreter.exec("import sys"); pyInterpreter.exec("print 'prefix', sys.prefix"); // pyInterpreter.exec("print sys.path"); System.out.println("python的jar包引用正確"); // pyInterpreter.exec("import sys"); // pyInterpreter.exec("print sys.path"); pyInterpreter.exec("import pymysql"); pyInterpreter.exec("print sys.prefix.pymysql.path"); // interpreter.exec("import pymysql"); // interpreter.exec("print pymysql.path"); // interpreter.exec("path = \"C:\\Users\\104005145\\Anaconda3\\Lib\\site-packages\""); // interpreter.exec("pymysql.path.append(path)"); /*interpreter.exec( "import pymysql\n" ); interpreter.exec( "import pandas as pd\n" ); interpreter.exec( "conn = pymysql.connect(host='127.0.0.1',user='root',password='root',database='metadata', port=3306, charset='utf8')\n" ); interpreter.exec( "cursor = conn.cursor()\n" ); interpreter.exec( "cursor.execute(\"select * from role\")\n" ); interpreter.exec( "rows = cursor.fetchall()\n" ); interpreter.exec( "data = pd.DataFrame(list(rows))\n" ); interpreter.exec( "print(data)\n" ); interpreter.exec( "data.to_csv(\"D:\\cq\\python\\text.csv\",encoding=\"utf_8_sig\")");*/ } }
相關文章
相關標籤/搜索