我有一个有4列数字的表,但是有些列有空白。在
当我试图读取表时,它会产生错误:
ValueError: could not convert string to float.
我解决了手工删除空白的错误,而且它是有效的,但是我想知道Python是否有一些解决方案来删除数字之间的空白
我删除空白的代码:
path = 'C:\Users\laboratorio\Desktop\waveletspy\prueba.txt'
path1 = 'C:\Users\laboratorio\Desktop\waveletspy\prueba1.txt'
clean_lines = []
with open(path, "r") as f:
lines = (line.strip() for line in f.readlines() if len(line.strip()))
clean_lines = [l.strip() for l in lines if l.strip()]
with open(path2, "w") as f:
f.writelines('\n'.join(clean_lines))
原始表格:
^{pr2}$粤2457620.83934-0.516 0.004
我想要这样的东西
Y 2457620.83012 -0.433 0.004
Y 2457620.83100 -0.439 0.005
Y 2457620.83518 -0.459 0.004
Y 2457620.83600 -0.470 0.005
Y 2457620.83684 -0.498 0.004
Y 2457620.83767 -0.480 0.005
Y 2457620.83851 -0.490 0.005
Y 2457620.83934 -0.516 0.004
你可以通过使用正则表达式来实现。它将删除所有不寻常的空白。在
代码:
结果:
^{pr2}$每行用空格隔开:
代码:
测试代码:
^{pr2}$结果:
可以使用正则表达式来执行此操作。首先用一个空格替换所有出现的多个空白字符,然后从每行的开头和结尾处修剪空白:
相关问题 更多 >
编程相关推荐