for循环中的字节到字符串错误

时间:2018-08-09 19:14:20

标签: python unicode byte

我调用了一个变量,并且我存储了值:

b'0,0.0372549,0.00653595,0,0.00490196,0.00653595,0.897386,0.00653595,0.0294118,0.00653595,0,0,0,0,0,0.00490196,0,0,0,0 \ n0,0.0158333,0.0179167 ,0.00277778,0.116111,0.327083,0.0265278,0.174167,0.0623611,0.116806,0.055,0.0270833,0,0,0.01875,0.0333333,0,0,0,0.00625 \ n0,0.997554,0.00133297,4.75327e-05,0.000118379,6.65359e -05,0.000253487,0.000141784,0.000183293,0.000220498,7.08961e-05,0,0,0,0,0,0,1.10967e-05,0,0,0 \ n0,0.082346,0.890084,0,0.000346861,0.0161043,0.0105323 ,0.000354988,0.00011562,0.00011562,0,0,0,0,0,0,0,0,0,0 \ n0,0.654957,0.306633,0,0.00150356,0.000466853,0.00273321,0.000933707,0.00233427,0.00186741,0,0 ,0,0,0.0190476,0.00952381,0,0,0,0 \ n0,0.429955,0.0543849,0.106071,0.0297159,0.0876241,0.16761,0.0435997,0.0293692,0.0167052,0.00492931,0.0143504,0.00664048,0.00127352,0.000520984,0.00405789,5.78871 e-05,0,0,0.00313481 \ n0,0.372212,0.459023,0.00909091,0.0183902,0.00273224,0.0897315,0.00666667,0.0163417,0.000546448,0.000208333,0.00929924,0.00666667,0,0,0,0,0,0,0,0,0.00,0.00909091 \ n0,0.916784,0.055 7399,0.00127288,0.0044239,0.00263795,0.00418008,0.00522153,0.00448012,0.0016537,0.0015471,0.00140777,0.00011532,4.21977e-05,4.21977e-05,5.76602e-05,0,0,0,0.000393295 \ n0,0.817223,0.10791 ,0.0119181,0.00617358,0.00279979,0.027469,0.00652177,0.00186348,0.00522515,0.0121172,0.000151166,0,0,0,0,0.00017316,0,0,0.000454545 \ n'

现在我正在编写以下代码:

for p in out.rstrip().split('\n'):
    print(p)

现在我遇到以下错误:

TypeError:需要一个类似字节的对象,而不是'str'

如何解决此错误?我尝试了unicoding,但不起作用。获取所需输出的for循环代码是什么。

2 个答案:

答案 0 :(得分:0)

将其永久更改为普通字符串以删除b:

out = out.decode("utf-8")

for p in out.rstrip().split('\n'):
    print(p)

答案 1 :(得分:0)

您正试图在'string'上使用“ split”,因此您基本上只需要删除'b',甚至在循环之前使用str(foo):

foo = '0,0.0372549,0.00653595,0,0.00490196,0.00653595,0.897386,0.00653595,0.0294118,0.00653595,0,0,0,0,0,0.00490196,0,0,0,0\n0,0.0158333,0.0179167,0.00277778,0.116111,0.327083,0.0265278,0.174167,0.0623611,0.116806,0.055,0.0270833,0,0,0.01875,0.0333333,0,0,0,0.00625\n0,0.997554,0.00133297,4.75327e-05,0.000118379,6.65359e-05,0.000253487,0.000141784,0.000183293,0.000220498,7.08961e-05,0,0,0,0,0,1.10967e-05,0,0,0\n0,0.082346,0.890084,0,0.000346861,0.0161043,0.0105323,0.000354988,0.00011562,0.00011562,0,0,0,0,0,0,0,0,0,0\n0,0.654957,0.306633,0,0.00150356,0.000466853,0.00273321,0.000933707,0.00233427,0.00186741,0,0,0,0,0.0190476,0.00952381,0,0,0,0\n0,0.429955,0.0543849,0.106071,0.0297159,0.0876241,0.16761,0.0435997,0.0293692,0.0167052,0.00492931,0.0143504,0.00664048,0.00127352,0.000520984,0.00405789,5.78871e-05,0,0,0.00313481\n0,0.372212,0.459023,0.00909091,0.0183902,0.00273224,0.0897315,0.00666667,0.0163417,0.000546448,0.000208333,0.00929924,0.00666667,0,0,0,0,0,0,0.00909091\n0,0.916784,0.0557399,0.00127288,0.0044239,0.00263795,0.00418008,0.00522153,0.00448012,0.0016537,0.0015471,0.00140777,0.00011532,4.21977e-05,4.21977e-05,5.76602e-05,0,0,0,0.000393295\n0,0.817223,0.10791,0.0119181,0.00617358,0.00279979,0.027469,0.00652177,0.00186348,0.00522515,0.0121172,0.000151166,0,0,0,0,0.00017316,0,0,0.000454545\n'

for p in foo.rstrip().split('\n'):
    print(p)

另一种解决方案是改为使用“样条线”,例如:

for p in foo.rstrip().splitlines():
    print(p)