繁体   English   中英

如何选择要从python字典中打印出的内容并将其写到另一个文件中?

[英]How can I choose what to print out from a python dictionary and write it out to another file?

我有一个读为“ peaks_ee.xpk”的文件,并且有一个字典,其中原子名称是键,化学位移是值。

这是我的peaks_ee.xpk文件的示例:

label dataset sw sf
1H 1H_2
NOESY_F1eF2e.nv
4807.69238281 4803.07373047
600.402832031 600.402832031
1H.L 1H.P 1H.W 1H.B 1H.E 1H.J 1H.U 1H_2.L 1H_2.P 1H_2.W 1H_2.B 1H_2.E 1H_2.J 1H_2.U vol int stat comment flag0 flag8 flag9
0 {1.H1'} 5.82020 0.05000 0.10000 ++ {0.0} {} {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
1 {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} {1.H1'} 5.82020 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
2 {1.H8} 8.13712 0.05000 0.10000 ++ {0.0} {} {1.H1'} 5.82020 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
3 {1.H1'} 5.82020 0.05000 0.10000 ++ {0.0} {} {1.H8} 8.13712 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
4 {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} {2.H1'} 5.90291 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
5 {2.H1'} 5.90291 0.05000 0.10000 ++ {0.0} {} {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
6 {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} {1.H1'} 5.82020 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
7 {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} {1.H8} 8.13712 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
8 {1.H1'} 5.82020 0.05000 0.10000 ++ {0.0} {} {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
9 {1.H8} 8.13712 0.05000 0.10000 ++ {0.0} {} {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0

这是python代码:

import pandas as pd

result = {}
text = 'fe'
filename = 'fe_yellow.xpk'

if text == 'ee':
    df = pd.read_csv('peaks_ee.xpk', sep=" ",skiprows=5)

    shift1= df["1H.P"]
    shift2= df["1H_2.P"]

    if filename == 'ee_pinkH1.xpk':
        mask = ((shift1>5.1) & (shift1<6)) & ((shift2>7) & (shift2<8.25))
    elif filename == 'ee_pinkH2.xpk':
    mask = ((shift1>3.25)&(shift1<5))&((shift2>7)&(shift2<8.5))

    result = df[mask]
    result = result[["1H.L","1H_2.L"]]

    tclust_atom = open("tclust.txt","a")

    tclust_atom.write(str(result))

输出为:

         1H.L    1H_2.L
25    {5.H2'}   {5.H1'}
26    {5.H2'}   {5.H1'}
27    {5.H2'}    {6.H5}
42    {7.H2'}   {7.H1'}
43    {7.H2'}   {7.H1'}
44    {7.H2'}    {8.H5}
60    {9.H2'}   {9.H1'}
61    {9.H2'}   {9.H1'}
62    {9.H2'}   {10.H5}
87   {12.H2'}  {12.H1'}
88   {12.H2'}  {12.H1'}
89   {12.H2'}   {13.H5}
132  {18.H2'}  {18.H1'}
133  {18.H2'}  {18.H1'}
146  {20.H2'}  {20.H1'}
147  {20.H2'}  {20.H1'}
154  {21.H2'}  {21.H1'}
155  {21.H2'}  {21.H1'}
169  {23.H2'}  {23.H1'}
170  {23.H2'}  {23.H1'}
171  {23.H2'}   {24.H5}

相反,我希望输出看起来像: Atom 1 5.H2' 5.H1' Atom 2 5.H2' 5.H1' Atom 3 5.H2' 6.H5 Atom 4 7.H2' 7.H1' Atom 5 7.H2' 7.H1' Atom 6 7.H2' 8.H5 Atom 7 9.H2' 9.H1' Atom 8 9.H2' 9.H1' Atom 9 9.H2' 10.H5 Atom 10 12.H2' 12.H1' Atom 11 12.H2' 12.H1' Atom 12 12.H2' 13.H5 Atom 13 18.H2' 18.H1' Atom 14 18.H2' 18.H1' Atom 15 20.H2' 20.H1' Atom 16 20.H2' 20.H1' Atom 17 21.H2' 21.H1' Atom 18 21.H2' 21.H1' Atom 19 23.H2' 23.H1' Atom 20 23.H2' 23.H1' Atom 21 23.H2' 24.H5

因此,我想摆脱第一行并摆脱当前文件中的花括号,并且要在每行旁边加上数字(从1到n)添加单词“ Atom”

例如,原子1和原子2相同,我怎么只能打印一次而不是两次?

检查这是否对您有帮助。 用以下代码替换代码中的最后两行:

for col in result.columns:
    result[col] = result[col].str.strip('{} ')
result.drop_duplicates(keep='first', inplace=True)
result = result.set_index([['Atom '+str(i) for i in range(1,len(result)+1)]])
tclust_atom = open("tclust.txt", "a")
result.to_string(tclust_atom, header=False)

for循环会删除DF中所有系列的多余空格和花括号。 drop_duplicatesdrop_duplicates从DF删除重复的行。 最后, set_index将整数索引替换为索引,其中每个条目的形式均为“ Atom#”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM