[英]how to remove a certain column after extraction, using python
I have a txt file, "as following:" 我有一个txt文件,“如下”:
.
.
.
-----------------------
MULLIKEN ATOMIC CHARGES
-----------------------
0 C : -0.157853
1 C : -0.156875
2 C : -0.143714
3 C : -0.140489
4 S : 0.058926
5 H : 0.128758
6 H : 0.128814
7 H : 0.142420
8 H : 0.140013
Sum of atomic charges: -0.0000000
------------------------
.
..
.
, and I have already extracted some of it, like this: ,而我已经提取了其中的一部分,如下所示:
stringy = []
with open('FILE.txt', 'rb') as f:
textfile_temp = f.read()
print textfile_temp.split('first ATOMIC CHARGES')[1].split("My charges : -0.0000000")[0].replace('-----------------------\n', '')
the result is good, but I want to remove the third column (" : ") [2] 结果很好,但是我想删除第三列 (“:”)[2]
0 C : -0.157853
1 C : -0.156875
2 C : -0.143714
3 C : -0.140489
4 S : 0.058926
5 H : 0.128758
6 H : 0.128814
7 H : 0.142420
8 H : 0.140013
This should do it: 应该这样做:
stringy = []
with open('FILE.txt', 'rb') as f:
textfile_temp = f.read()
textfile_temp = textfile_temp.split('MULLIKEN ATOMIC CHARGES')[1].split("Sum of atomic charges: -0.0000000")[0].replace('-----------------------\n', '')
for line in textfile_temp.split("\n"):
print(line.replace(' : ', ''))
But I'm not able to test it as your example string you put does not match the split
you have in your code, as eg MULLIKEN ATOMIC CHARGES
is not in the string. 但是我无法对其进行测试,因为您输入的示例字符串与代码中的split
不匹配,例如,字符串中没有MULLIKEN ATOMIC CHARGES
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.