在python中使用split()和join()时保留空格 [英] Preserve whitespaces when using split() and join() in python
问题描述
我有一个数据文件,其中包含类似
I have a data file with columns like
BBP1 0.000000 -0.150000 2.033000 0.00 -0.150 1.77
,各个列之间以不同数量的空格分隔.
and the individual columns are separated by a varying number of whitespaces.
我的目标是读这些行,对几行进行一些数学运算,例如将第4列乘以0.95,然后将它们写到新文件中.新文件应该看起来像原始文件,除了我修改的值.
My goal is to read in those lines, do some math on several rows, for example multiplying column 4 by .95, and write them out to a new file. The new file should look like the original one, except for the values that I modified.
我的方法是将行作为列表项读取.然后,在感兴趣的行上使用split()
,这将为我提供一个包含各个列值的子列表.然后,我进行修改,将各列一起join()
,并将列表中的行写入新的文本文件.
My approach would be reading in the lines as items of a list. And then I would use split()
on those rows I am interested in, which will give me a sublist with the individual column values. Then I do the modification, join()
the columns together and write the lines from the list to a new text file.
问题是我有那些数量不等的空白.我不知道如何像阅读它们一样介绍它们.我唯一想到的方法是在分割字符之前先对行中的字符进行计数,这将非常繁琐.有人有更好的主意来解决这个问题吗?
The problem is that I have those varying amount of whitespaces. I don't know how to introduce them back in the same way I read them in. The only way I could think of is to count characters in the line before I split them, which would be very tedious. Does someone have a better idea to tackle this problem?
推荐答案
在这种情况下,您想将re.split()
与一个组一起使用:
You want to use re.split()
in that case, with a group:
re.split(r'(\s+)', line)
将同时返回列和的空白,因此您以后可以在包含相同数量的空白的情况下重新加入该行.
would return both the columns and the whitespace so you can rejoin the line later with the same amount of whitespace included.
示例:
>>> re.split(r'(\s+)', line)
['BBP1', ' ', '0.000000', ' ', '-0.150000', ' ', '2.033000', ' ', '0.00', ' ', '-0.150', ' ', '1.77']
您可能要做要从最后删除换行符.
You probably do want to remove the newline from the end.
这篇关于在python中使用split()和join()时保留空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!