Frage

I was trying to run a python script through DataNitro in excel by importing the script and then pressing the runn button. I got this errormessage:

Traceback (most recent call last):
  File "27/scriptStarter.py",line 97,in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe5' in position 52:
 ordinal not in range(128)

So instead I just copy pasted the script into the Python shell for DataNitro by using the %paste function, and it worked.

Why did it work in the shell and not in the script? I haven't even seen the file 27/scriptstarter.py when i did look for it. I really don't know a lot about these things, but is it a translation between unicode and ascii that got wrong?

Here follows my tiny little script, if it helps any of you guys

i=3
while i<4051:
    if 337 <=Cell(i,15).value<=23 and Cell(i,3).value>=1:
        Cell(i,19).value=1
    if 23 <=Cell(i,15).value<=68 and Cell(i,4).value>=1:
        Cell(i,19).value=1
    if 68 <=Cell(i,15).value<=113 and Cell(i,5).value>=1:
        Cell(i,19).value=1
    if 113 <=Cell(i,15).value<=158 and Cell(i,6).value>=1:
        Cell(i,19).value=1
    if 158 <=Cell(i,15).value<=203 and Cell(i,7).value>=1:
        Cell(i,19).value=1
    if 203 <=Cell(i,15).value<=248 and Cell(i,8).value>=1:
        Cell(i,19).value=1
    if 248 <=Cell(i,15).value<= 293 and Cell(i,9).value>=1:
        Cell(i,19).value=1
    if 293 <=Cell(i,15).value<=337 and Cell(i,10).value>=1:
        Cell(i,19).value=1
    i=i+1
War es hilfreich?

Lösung

The script you pasted doesn't seem to have any unicode in it, so it should run without a problem. If you're still having trouble running it when importing, try putting this line at the top of the file:

# -*- coding: utf-8 -*-

This tells Python that the source file has unicode encoding, rather than ascii, and should solve the issue.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top