简体   繁体   English

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

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

I have a file I read in called 'peaks_ee.xpk' and I have a dictionary in which the atom name is the key and the chemical shift is the value. 我有一个读为“ peaks_ee.xpk”的文件,并且有一个字典,其中原子名称是键,化学位移是值。

This is a sample of my peaks_ee.xpk file: 这是我的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

This is the python code: 这是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))

The output is: 输出为:

         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}

Instead, I want the output to look like: 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 相反,我希望输出看起来像: 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

So I want to get rid of the first line and get rid of the curly braces in the file I currently have, and I want to add the word "Atom" next to each line along with the number (starting from 1 going to n) 因此,我想摆脱第一行并摆脱当前文件中的花括号,并且要在每行旁边加上数字(从1到n)添加单词“ Atom”

And for example, atom 1 and atom 2 are the same, how can I only print it once instead of twice? 例如,原子1和原子2相同,我怎么只能打印一次而不是两次?

Check if this helps you. 检查这是否对您有帮助。 Replace the last two lines in your code with this code: 用以下代码替换代码中的最后两行:

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)

The for loop strips the extra spaces and curly braces from all the series in your DF. for循环会删除DF中所有系列的多余空格和花括号。 drop_duplicates , as the name suggests, drops duplicate rows from the DF. drop_duplicatesdrop_duplicates从DF删除重复的行。 And finally set_index replaces the integer index with the index where each entry is of the form 'Atom #'. 最后, set_index将整数索引替换为索引,其中每个条目的形式均为“ Atom#”。

暂无
暂无

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

相关问题 如何从另一个python字典中的一个字典中获得相应的列表值,在列表中它们被列为键,比较并打印出csv? - How can I get corresponding list values in one dictionary from another python dictionary where they are listed as keys, compare and print out a csv? 从文件中打印字典 - Print out dictionary from file 仅当某些值在范围内时,我才能从文件中读取内容,然后写出到另一个文件中? - How can I read in from a file, then write out to another file only if certain values are in a range? python:如何打印我的文件行? - python:How can I print out my file lines? 如何在python中打印出txt文件的字典值的整个列表 - how to print out WHOLE list of dictionary values of txt file in python 当我的Python字典中出现KeyError时,如何打印出某些内容? - How can I print out something when I have KeyError for my dictionary in Python? Python中是否有一种方法可以获取列表并从字典中打印出相应的值? - Is there a method in Python where I can take a list, and print out the corresponding value from a dictionary? 如何在 PYTHON 中打印出字典中键的内容? - How do I print out contents of a Key in a dictionary in PYTHON? 如何从 python 中的字典中打印出格式良好的表格 - How to print out nicely formatted tables from a dictionary in python 如何让 python 使用字典打印出正确的 output ? - How can I let python print out the right output using a dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM