I have a C# application in which I use to run a Python script using accented characters (ÀÉÈÏ etc.).
To do that I use Iron Python. My script works fine with Python 3.5.1 (in the IDLE) but since Iron Python execute it as a Python 2.7 script my script won't work.
I've read that it is the encoding that should be Unicode instead of Utf8 but the only relevant thing I've found while looking on Google is to either put a u before the string (i.e. text = u'theText') or to use unicode() (i.e. unicode(theText) ). Both of them did not work.
I get the error : 'Microsoft.Scripting.SyntaxErrorException'
from the IronPython.dll.
More specific : Non-ASCII character '\xc3' in file pathToScript/cesar.py on line 13, but no encoding declared
.
Here is a simplified version of my Python script which does a Caesar cipher "encryption" :
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
And here is how I would call it in my C# application :
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);
So here it is, how do I make my letters variable in my Python script work in a Python 2.7 environment (Iron Python) ?