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

Python如何删除数字之间的空白,python,numbers,whitespace,tabular,Python,Numbers,Whitespace,Tabular,我有一个包含4列数字的表,但是在某些列中有空格 当我尝试读取表格时,它会产生错误: ValueError:无法将字符串转换为浮点 我解决了手动删除空白的错误,它是有效的,但是我想知道Python是否有一些解决方案来删除数字之间的空白 删除空白的我的代码: path = 'C:\Users\laboratorio\Desktop\waveletspy\prueba.txt' path1 = 'C:\Users\laboratorio\Desktop\waveletspy\prueba1.txt'

我有一个包含4列数字的表,但是在某些列中有空格

当我尝试读取表格时,它会产生错误:

ValueError:无法将字符串转换为浮点

我解决了手动删除空白的错误,它是有效的,但是我想知道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))
原始表格:

 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

我想要这样的东西

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

要使每行中的每个单词各用一个空格隔开:

代码: 测试代码: 结果:
您可以使用正则表达式来实现这一点。首先用一个空格替换所有出现的多个空格字符,然后从每行的开头和结尾修剪空格:

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

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

代码:

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')
结果:

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)
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')
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