Python如何删除数字之间的空格

2024-10-03 23:28:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个有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

Tags: pathtxtclean错误withline数字open
3条回答

你可以通过使用正则表达式来实现。它将删除所有不寻常的空白。在

代码:

import re
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:
        for i in clean_lines:
            f.writelines(re.sub(r'\s+', ' ', str(i)))
            f.writelines('\n')

结果:

^{pr2}$

每行用空格隔开:

代码:

clean_lines = [' '.join([w.strip() for w in l.strip().split()])
               for l in lines if l.strip()]

测试代码:

^{pr2}$

结果:

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

可以使用正则表达式来执行此操作。首先用一个空格替换所有出现的多个空白字符,然后从每行的开头和结尾处修剪空白:

import re
with open(path, 'r') as f:
    result = re.sub(r'(^ | $)', '', re.sub(r'\w+', ' ', f.read()))
    print(result)

相关问题 更多 >