读取处理中的python

2024-10-03 06:21:57 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何在不使用的情况下读取python文件吗py处理或其他第三方库或平台?我有一个python文件,可以生成一个文本,并希望我的处理实时读取它。但是“loadStrings”的东西似乎有问题,因为我的三行文本不是同时生成的,我的第三行总是比前两行慢一点,所以处理草图在某个时候把它弄乱了。如何处理这个问题?在

String[] lines;
PFont font;

void setup() {
  size(800, 600);
  font = createFont("Arial", 16);
  frameRate(2);
  //lines = loadStrings("output.txt");
}

void draw() {
  background(255);
  textFont(font);
  fill(0);
  lines = loadStrings("output.txt");
  for (int i = 0; i < 3; i++) {

    String word = lines[i];

    text(word, random(width), random(height));
  }
 // noLoop();
}

我的Python素描:

^{pr2}$

Tags: 文件py文本txtoutputstring情况random
1条回答
网友
1楼 · 发布于 2024-10-03 06:21:57

您可以使用java的运行时和Process类:

  import java.io.*;

  void setup() {
    try {
      Process p = Runtime.getRuntime().exec("python /path/to/your/script.py arguments");

      BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

      String line=null;

      while((line=input.readLine()) != null) {
          System.out.println(line);
      }

      int exitWith = p.waitFor();
      System.out.println("Exited with error code "+exitWith);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

比如说10.py公司名称:

^{pr2}$

我会看到一条信息打印了10次:

  import java.io.*;

  void setup() {
    try {
      Process p = Runtime.getRuntime().exec("python /Users/hm/Documents/Processing/tests/CMD/ten.py hello");

      BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

      String line=null;

      while((line=input.readLine()) != null) {
          System.out.println(line);
      }

      int exitWith = p.waitFor();
      System.out.println("Exited with error code "+exitWith);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

相关问题 更多 >