Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python随机unicode字符串_Python_Unicode - Fatal编程技术网

Python随机unicode字符串

Python随机unicode字符串,python,unicode,Python,Unicode,我不熟悉python和unicode。我试图在python中生成一个随机的unicode字符串。这就是我想到的: def randomUnicodeStr(self, ustart, uend): '''Generate a random unicode string whose length is strlen and whose unicode characters are in range ustart, uend''' strlen = 5 uchars = [r

我不熟悉python和unicode。我试图在python中生成一个随机的unicode字符串。这就是我想到的:

def randomUnicodeStr(self, ustart, uend):
    '''Generate a random unicode string whose length is strlen and whose unicode characters are in range ustart, uend'''
    strlen = 5
    uchars = [random.choice(range(ustart, uend+1)) for _ in range (strlen)]
    ustr   = u''
    for uc in uchars:
        ustr += unichr(uc)

    #for i in range(len(ustr)):
    #    print "Py char " + str(i) + " = " + str(ord(ustr[i]))

    return ustr

我无法从python文档中收集这些信息,但是上面我的
ustr
使用了什么编码?

根据定义,Unicode字符串没有编码。编码是将字符串转换为字节以写入设备时发生的事情。@MarkRansom:RAM也是一个设备,因此必须以某种方式对其进行编码。它相信它是最新python中的UCS-4,然而,这一事实对于python应用程序开发人员来说是完全无关的。接下来的一个问题是:你到底想用字符串做什么,为什么你认为你需要知道编码?你把这个问题的代码完全适合你的目标。@马兰索姆:我们有一个设置,我们的回归测试是用Python编写的,并且调用我们的C++代码。用这个函数生成的Unicode字符串被用作C++函数的参数,它接收这个字符串作为ICU::UnicodeString。当我遍历C++侧的字符并打印它们的代码值时,我发现它们与我在Python函数中设置的代码值相匹配。由于我是unicode新手,我想知道是否需要关注编码,因为这个字符串跨越了语言边界。看起来不是这样,我想没关系。然后,您需要的是传递给C API的表示,它可能不同于内部表示(这是一个您不应该关心的实现细节)。例如,见。