小编Phi*_*hil的帖子

如何检测字符串字节编码?

我有大约1000个文件名读取os.listdir(),其中一些以UTF8编码,一些是CP1252.

我想将它们全部解码为Unicode,以便在我的脚本中进一步处理.有没有办法让源编码正确解码为Unicode?

例:

for item in os.listdir(rootPath):

    #Convert to Unicode
    if isinstance(item, str):
        item = item.decode('cp1252')  # or item = item.decode('utf-8')
    print item
Run Code Online (Sandbox Code Playgroud)

python string unicode encoding byte

31
推荐指数
3
解决办法
7万
查看次数

哈希表和关键订单

有没有办法在添加哈希表时保持键的顺序?像推/弹机制一样.

例:

$hashtable = @{}

$hashtable.Add("Switzerland", "Bern")
$hashtable.Add("Spain", "Madrid")
$hashtable.Add("Italy", "Rome")
$hashtable.Add("Germany", "Berlin")
$hashtable
Run Code Online (Sandbox Code Playgroud)

我想保留我将元素添加到哈希表的顺序.

sorting powershell hashtable powershell-2.0

29
推荐指数
3
解决办法
3万
查看次数