我是C#和Python的新手,我无法解决尝试从C#执行Python脚本的问题。我的问题与以下链接中发布的问题完全相同:
我在另一个网站上找到了答案,他们建议在Python中用双引号引起来的目录路径。好吧,我修复了它,但是问题仍然是,“意外的令牌'追加'”。
我的C#代码如下:
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile(@"/Users/alberto/Desktop/Csharp/test_corso/scacchi/scacchiera.py");
ICollection<string> paths = engine.GetSearchPaths();
paths.Add(@"/Users/alberto/anaconda3/lib/python3.7/site-packages");
paths.Add(@"/Users/alberto/anaconda3/lib/python3.7");
engine.SetSearchPaths(paths);
ScriptScope scope = engine.CreateScope();
我的Python代码是:
import numpy as np
import pytz
import pandas as pd
chess=pd.read_csv("/Users/alberto/Desktop/Csharp/test_corso/scacchi/scacchiera.txt",names=['nome','colore','indice','stato','x','y'],header=None)
chess_occ=chess[chess['stato']=='ok']
chess_occ=chess_occ.drop_duplicates(['nome','colore','indice'],keep='last')
chess_tail=chess.tail(1)
chess_merge=pd.merge(chess_occ,chess_tail,on=['x','y'],suffixes=['','_'])
a=pd.merge(chess_merge,chess_tail,on=['nome','colore','indice'],how='outer',suffixes=['','_t'],indicator=True)
a=a[a['_merge']=='left_only']
a=a[['nome','colore','indice','stato','x','y','colore_']]
for i in a.index:
if a['colore'][i]!=a['colore_'][i]:
a['stato'][i]='ko'
b=pd.merge(chess,a,how='left',on=['nome','colore','indice'],suffixes=['','_new'])
b=b.drop(['x_new','y_new','colore_'],axis=1)
for i in b.index:
if b['stato_new'][i]=='ko':
b['stato'][i]='ko'
b=b.drop(['stato_new'],axis=1)
b.to_csv("/Users/alberto/Desktop/Csharp/test_corso/scacchi/scacchiera.txt",index=False,header=False)
occupati=b.drop_duplicates(['nome','colore','indice'],keep='last')
occupati=occupati[occupati['stato']=='ok']
occupati=occupati[['x','y']]
occupati.to_csv("/Users/alberto/Desktop/Csharp/test_corso/scacchi/occupati.txt",index=False,header=False)
source.Execute(scope);
我在哪里错了?非常感谢你! 再见,古弗斯