我从TCP端口读取了一个字符串数据,它看起来像这样:

data_string = '1,1.0,2.4,4,3,0.0,0.0,0.0,7,-2.5,80,1481307702.180;2,3.3,10.1,2,2,0.0,-0.0625,-0.25,7,-20.0,3,1481307702.180;3,-0.2,1.5,4,4,0.0,0.0,0.0,7,-7.0,80,1481307702.180;4,-3.7,25.4,2,1,-0.0625,0.0,0.0,7,19.5,3,1481307702.180;5,-3.2,5.8,4,4,-0.0625,0.0,0.0,7,-5.0,39,1481307702.180;6,2.2,13.4,2,2,0.0,0.0,-0.25,6,-24.5,3,1481307702.180;7,-2.4,19.9,2,1,0.0,0.0625,0.0,7,-23.0,5,1481307702.180;8,0.0,0.0,0,0,0.0,0.0,0.0,1,0.0,0,1481307702.180;9,0.6,7.7,4,3,0.0625,0.0625,0.0,7,-18.5,6,1481307702.180'


我使用np.matrix(data_string)将数据放入numpy矩阵。

在对数据进行了一些处理之后,我想将其放回相同的字符串格式。有什么建议吗?

最佳答案

如果m是矩阵,则以下内容会将其转换为指定格式的字符串:

new_string =  ";".join([",".join(map(str, row)) for row in m.tolist()])


我对您的数据进行了测试;输出几乎与原始data_string相同,除了1变为1.0(但无论如何您的1都是浮点数,所以应该没问题)。

关于python - 字符串转换为np.matrix返回字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41105033/

10-12 17:35