我有一个C#应用程序,我在其中使用重音字符(ÀÉÈÏ等)运行Python脚本。
为此我使用Iron Python。我的脚本适用于Python 3.5.1(在IDLE中),但由于Iron Python将其作为Python 2.7脚本执行,因此我的脚本无效。
我已经读过它应该是Unicode而不是Utf8的编码,但我在Google上看到的唯一相关的事情是要么放 u 在字符串之前(即text = u'theText')或使用 unicode() (即unicode(theText))。他们俩都没有用。
我从IronPython.dll收到错误:'Microsoft.Scripting.SyntaxErrorException'
。
更具体:Non-ASCII character '\xc3' in file pathToScript/cesar.py on line 13, but no encoding declared
。
这是我的Python脚本的简化版本,它使用凯撒密码“加密”:
class WhateverName:
def index(self, lettre, texte):
for k in range(len(texte)):
if (lettre == texte[k]):
return k
return -1
def rotateLetters(self, text , number):
letters = 'abcdefghijklmnopqrstuvwxyzéèëêàâöôçîïü!?.,'
lettersRotation = letters[number:] + letters[:number]
newText = ''
for letter in text:
newText = newText + lettersRotation[self.index(letter, letters)]
return newText
以下是我在C#应用程序中调用它的方法:
ScriptEngine engine = Python.CreateEngine();
string pythonScriptPath = System.IO.Path.GetDirectoryName(System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
ScriptSource source = engine.CreateScriptSourceFromFile(pythonScriptPath + "/cesar.py");
ScriptScope scope = engine.CreateScope();
source.Execute(scope);
Object myclass = engine.Operations.Invoke(scope.GetVariable("WhateverName"));
object[] parameters = new object[] { text, number };
encryptedText += engine.Operations.InvokeMember(myclass, "rotateLetters", parameters);
所以在这里,我如何在我的Python脚本中使用字母变量在Python 2.7环境中工作(Iron Python)?