简体   繁体   English

如何使用特殊字符作为字符串的“对象”类型变量?

[英]How to work 'Object' type variable with special character as string?

I am doing some testing of a text UI element from a Qt application.我正在对来自 Qt 应用程序的文本 UI 元素进行一些测试。 It has a special character in it.它有一个特殊的字符。 When I try to get the text with Squish the received value type is Object.当我尝试使用 Squish 获取文本时,接收到的值类型是 Object。 The final goal is to do some operations on it like printing it out or comparing it with another string.最终目标是对其进行一些操作,例如将其打印出来或与另一个字符串进行比较。 It's also completely fine to rid of the special character and only look at the remaining value.去掉特殊字符,只看剩余值也完全没问题。

In an effort to find out what I can do with this value, I've tried the following:为了找出我可以用这个值做什么,我尝试了以下方法:

  1. value.split('')

    SyntaxError: Ambiguous overload 'split(str)'. SyntaxError:模棱两可的重载'split(str)'。 Candidates: QString::split(const QString & sep) QString::split(QChar sep)候选: QString::split(const QString & sep) QString::split(QChar sep)

  2. str(value)

    UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 2: ordinal not in range(128) UnicodeEncodeError:'ascii' 编解码器无法在 position 2 中编码字符 u'\xb0':序数不在范围内(128)

  3. value.encode('utf-8')

    AttributeError: Object does not have any properties AttributeError: Object 没有任何属性

  4. unicode(value, error='replace')

    TypeError: coercing to Unicode: need string or buffer, Object found类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到 Object

Usually in other case I can use str() fine since there is no special character.通常在其他情况下,我可以很好地使用 str(),因为没有特殊字符。 This is from Python 2, and upgrading is not really an option since it's quite a big project and takes time.这是来自 Python 2,升级不是一个真正的选择,因为它是一个相当大的项目并且需要时间。 Please give me some suggestion if this can be done in anyway.如果无论如何都可以这样做,请给我一些建议。 Thank you.谢谢你。

value.decode("ascii", errors="ignore").encode() value.decode("ascii", errors="ignore").encode()

So using dir() on that value I find out that QString function can be used.因此,在该值上使用dir()我发现可以使用 QString function 。 After some more experimenting I find QString &QString::replace(int position, int n, const QChar *unicode, int size) as the only method that I can use without running into error Ambiguous overload.经过更多试验后,我发现QString &QString::replace(int position, int n, const QChar *unicode, int size)是我可以使用的唯一方法而不会遇到错误模糊重载。 Then just replacing the special character away and use the remaining value.然后只需将特殊字符替换掉并使用剩余的值。

I cannot reproduce this problem with Squish for Qt 6.5.x, Python 2.7:对于 Qt 6.5.x、Python 2.7,我无法使用 Squish 重现此问题:

import os

def main():
    startApplication("%s/examples/qt/addressbook/addressbook" % os.getenv("SQUISH_PREFIX"))
    o = waitForObject({"type": "MainWindow"})
    o.windowTitle = "ä"
    str(o.windowTitle)

(Posted as an answer since a comment does not seem to support multiple lines, but not meant as an answer.) (作为答案发布,因为评论似乎不支持多行,但并不意味着作为答案。)

Try to decode value with Qt:尝试使用 Qt 解码值:

value.toUtf8().constData()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM