Jana Mizikova Posted September 18, 2018 Report Share Posted September 18, 2018 Hello, recently I've been working on the custom monthly tasks report via Python API (1.7.0). And it fails everytime when tasks are created by managers with special characters in a task name, otherwise it works fine on its own. I don't want to manually rename the task in the web UI everytime it happens. I wanted to fix it in my script with exception handling block for UnicodeEncodeError. But for some reason I can't even query the task from the session to replace the wrong character in my script. I get this error: >>> session.query('Task where parent.name is "Test Subjects" and type is "R&D"').one() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "P:\.sys\python\Python27\lib\site-packages\ftrack_api\query.py", line 164, in one results, metadata = self._session._query(expression) File "P:\.sys\python\Python27\lib\site-packages\ftrack_api\session.py", line 821, in _query results = self._call(batch) File "P:\.sys\python\Python27\lib\site-packages\ftrack_api\session.py", line 1637, in _call raise ftrack_api.exception.ServerError(error_message) ftrack_api.exception.ServerError: Server reported error: AttributeError('unicode' object has no attribute '_sa_instance_state') I can query the task if conditions result in getting multiple tasks from the query, but when it gets to passing the name of the task for further processing, I get this: UnicodeEncodeError: 'ascii' codec can't encode character u'\u0161' in position 28: ordinal not in range(128) Am I doing something wrong here? I don't understand how and why it is even possible to create a task with a special character in the task name if it's using unicode as a variable type and then it cannot be queried. Is there another way how to query the task from session? Can I change encoding in the process of reading a task name? Thanks in advance for the reply! - Jane Link to comment Share on other sites More sharing options...
tomas Posted September 20, 2018 Report Share Posted September 20, 2018 Hi Jana! if you want select task by type, you must use "type.name" in query because "type" is an object and you want use name of object in filter. So you can change your query to: session.query('Task where parent.name is "Test Subjects" and type.name is "R&D"').all() And Unicode error you get mean the name of your task contain some utf8 character ('š' I guess) and API return you not "string" object but "unicode". print type(your_task["name"]) >> <type 'unicode'> If you try cast unicode with utf character to string, you will get unicode error (https://stackoverflow.com/questions/18034272/python-str-vs-unicode-types)...So you can try following: print type(your_task["name"].encode("utf-8")) >> <type 'str'> and you get string answer from stackoverflow: https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20 Link to comment Share on other sites More sharing options...
Jana Mizikova Posted September 24, 2018 Author Report Share Posted September 24, 2018 Thanks for the reply ! I thought those two errors are connected and caused by one thing but apparently I did two separate mistakes there : ) The query works fine now. I fixed also the part with encoding to utf-8 and works smoothly. Thank you very much ! - Jana Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.