问题
I wrote a simple language translator application using IronPython and I call the python code via a C# console application. I tested the python code separately and it works fine. However, when I call the python code via the C# application it always gives the Microsoft.Scripting.SyntaxErrorException: 'unexpected token 'append'' error message.
ScriptEngine engine = Python.CreateEngine();
ScriptSource source = engine.CreateScriptSourceFromFile(@"D:\Projects\Translator.py");
ICollection<string> Paths = engine.GetSearchPaths();
Paths.Add(@"C:\Program Files\Python37\Lib\");
Paths.Add(@"C:\Program Files\Python37\Lib\site-packages\");
engine.SetSearchPaths(Paths);
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
dynamic GoogleTranslator = scope.GetVariable("GoogleTranslator");
dynamic gTranslator = GoogleTranslator();
gTranslator.SetInfo("'Привет, корова мальчик'");
var result = gTranslator.Translate();
Console.Write(result);
Console.Read();
---------- Python Code -----------
from googletrans import Translator
class GoogleTranslator(object):
Text = ""
Dest = ""
def SetInfo(self, text, dest = 'en'):
self.Text = text
self.Dest = dest
def Translate(self):
translator = Translator()
result = translator.translate(self.Text, self.Dest)
return result.text
Can anyone tell me what is the issue with the code. The error generate from the "source.Execute(scope);"
来源:https://stackoverflow.com/questions/52281491/unexpected-token-append