1

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) ?

Marks
  • 165
  • 4
  • 19
  • 1
    https://www.python.org/dev/peps/pep-0263/ is probably the most useful resource. tl;dr add `# -*- coding: utf-8 -*-` (or whatever your encoding is) to the top of your file – anthony sottile May 12 '16 at 05:03
  • @AnthonySottile Using the `# -*- coding: utf-8 -*-` makes that my IronPython won't crash, which is great though I still get these ( � ) out of my script when it's an accented character. I tried to put Unicode which is the default of Python 3.X from what I've read but it says ***Unknown encoding: unicode. Saving as UTF-8***. So no matter what type of encoding I try else than utf-8 it does not recognize it and save the file as utf-8 anyways... Any idea why ? – Marks May 12 '16 at 15:05
  • As additional notes, I tried latin-1& iso-8859-15 which did save the document as these encoding format but any ascii or unicode did not work and made it save as utf-8. In all these cases, the accented characters still are not being recognized properly ( � ). – Marks May 12 '16 at 15:52
  • You'll also need to `u''` prefix your strings (to make them text strings). In python2, string literals by default are bytes literals – anthony sottile May 12 '16 at 17:54
  • @AnthonySottile I forgot to mention it but that's what I did. But with or without the prefix ***u*** it stays �. Is it because the `# -*- coding: utf-8 -*-` is utf-8 ? Because I tried to put it in ascii and in unicode but, like I said, it doesn't let me put these (i.e. `# -*- coding: unicode -*-` or `# -*- coding: ascii -*-` ) – Marks May 12 '16 at 20:21

0 Answers0