JAVA调用PY出现的关于IDE的问题

178 阅读1分钟

先贴PY

import jieba
import sys
##清洗

# 创建停用词列表
def stopwordslist():
    stopwords = [line.strip() for line in open('D:\pythonplace\spider\weibo\stopword.txt', encoding='UTF-8').readlines()]
    return stopwords


# 对句子进行中文分词
def seg_depart(sentence):

# 给出文档路径
def giveres(filename, outfilename):
    inputs = open(filename, 'r', encoding='UTF-8')
    outputs = open(outfilename, 'w', encoding='UTF-8')
    # 将输出结果写入out.txt中 就这里出现外部调用就返回空白文件
    for line in inputs:
        # line = re.sub(r'^\w+\s*', '', line)
       # print(inputs)
        line_seg = seg_depart(line)
       # print(line_seg)
        if (len(line_seg) > 0):
            outputs.write(line_seg + '\n')
    # print("----------------正在分词和去停用词-----------")
    outputs.close()
    inputs.close()
    print("删除停用词和分词成功!!!")


if __name__ == '__main__':
    filename = sys.argv[1]
    outfilename = sys.argv[2]
    giveres(filename, outfilename)

然后是JAVA调用环节

public void jieba(String path,String filename) {
		
		String a=path+filename+".txt";
		String b=path+filename+"_jieba.txt";
			try { 
				//外部调用出现空白文件结果 原因是文件路径必须写死
			    String[] arguments = new String[] { "python", "D:\\pythonplace\\spider\\weibo\\clean.py"};
			    Process process = Runtime.getRuntime().exec(arguments);
	            int re = process.waitFor();

			} catch (IOException e) {
			    e.printStackTrace();
			} catch (InterruptedException e) {
			    e.printStackTrace();
			}
			return ;
	}

发现导出文件一直是空白的 网上冥思苦想了一会 发现问题在Py IDE的问题

stopwords = [line.strip() for line in open('\stopword.txt', encoding='UTF-8').readlines()]
这是错误的代码
然后在py中是可以直接使用的
原因在于IDE自动帮你补齐了前面空缺的位置
然后java外部调用py就会绕过PY IDE? 
没有对地址补齐
需要直接把文件位置写死
d:\\path\\file.txt

开发时总会出现奇奇怪怪的问题