![](/img/trans.png)
[英]How can I create a python dictionary that gets its values and keys from the user through tkinter text boxes?
[英]How can I create a python dictionary using values from a text file that I'm reading in
我有一個文件“ peaks_ee.xpk”,我正在嘗試使用該文件中的值在python代碼中創建一個字典。
j = 0;
contents_atom = []
atom_lines=[]
with open ("peaks_ee.xpk","r") as atomName:
for name in atomName.readlines():
float_str = re.findall("\d\.H\d'?", name)
if (len(float_str)>1):
j = j+1
value1 = ('Atom ' + str(j) + ' ' + str(float_str[0]) + ' ' + str(float_str[1]) + '\n')
atom_lines.insert(-1,value1)
tclust_atom = open("tclust.txt","a")
for value1 in atom_lines:
tclust_atom.write(value1)
tclust_atom.close()
我正在讀取文件peaks_ee.xpk。 這是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
10 {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} {4.H1'} 5.74125 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
11 {4.H1'} 5.74125 0.05000 0.10000 ++ {0.0} {} {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
12 {3.H1'} 5.54935 0.05000 0.10000 ++ {0.0} {} {4.H8} 7.49932 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
13 {4.H8} 7.49932 0.05000 0.10000 ++ {0.0} {} {3.H1'} 5.54935 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
14 {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} {3.H1'} 5.54935 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
15 {3.H1'} 5.54935 0.05000 0.10000 ++ {0.0} {} {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
16 {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} {2.H1'} 5.90291 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
17 {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
18 {2.H1'} 5.90291 0.05000 0.10000 ++ {0.0} {} {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
19 {2.H8} 7.61004 0.05000 0.10000 ++ {0.0} {} {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
20 {4.H8} 7.49932 0.05000 0.10000 ++ {0.0} {} {4.H1'} 5.74125 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
21 {4.H1'} 5.74125 0.05000 0.10000 ++ {0.0} {} {4.H8} 7.49932 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
22 {4.H8} 7.49932 0.05000 0.10000 ++ {0.0} {} {3.H1'} 5.54935 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
23 {4.H8} 7.49932 0.05000 0.10000 ++ {0.0} {} {3.H6} 7.53261 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
24 {3.H1'} 5.54935 0.05000 0.10000 ++ {0.0} {} {4.H8} 7.49932 0.05000 0.10000 ++ {0.0} {} 0.0 100.0000 0 {} 0 0 0
我想制作一個以原子名稱為鍵的字典。 peaks_ee.xpk中的原子名稱是“ 1.H1'”,“ 2.H8”等。我希望該值是化學位移,例如“ 5.82020”和“ 7.61004”(即將到來)從peaks_ee.xpk中的0行開始)因此,例如,我希望字典看起來像:
dict = { "1.H1'":"5.82020", "2.H8":"7.61004"...}
但是,下一行通過再次具有2.H8和1.H1'來重復,因此不需要將其添加到字典中。 該行之后的行(第2行)應添加到字典中,因為它有一個名為1.H8的新原子,因此應為
dict = {"1.H1'":"5.82020", "2.H8":"7.61004", "1.H8:8.13712", ...}
我怎樣才能做到這一點?
編輯:如果我還有另一個文件“ ee_pinkH1.xpk”,我想讀入它,看看從那里的化學位移值是否在一定范圍內,然后打印出這些值,這是代碼嗎?
這是我的整個代碼:
import os
import sys
import re
i = 0;
contents_peak = []
peak_lines=[]
with open ("ee_pinkH1.xpk","r") as peakPPM:
for PPM in peakPPM.readlines():
float_num = re.findall("[\s][1-9]{1}\.[0-9]+",PPM)
if (len(float_num)>1):
i=i+1
value = ('Peak ' + str(i) + ' '+ str(float_num[0])+ ' 0.05 ' + str(float_num[1])+ ' 0.05 ' + '\n')
peak_lines.insert(-1,value)
tclust_peak = open("tclust.txt","w+")
tclust_peak.write('rbclust \n')
for value in peak_lines:
tclust_peak.write(value)
tclust_peak.close()
j = 0;
contents_atom = []
atom_lines=[]
result = {}
with open ("peaks_ee.xpk","r") as atomName:
for name in atomName.readlines():
for match in rex.finditer(line):
name,shift = match.groups()
if name not in result:
result[name] = float(shift)
float_str = re.findall("\d\.H\d'?", name)
if (len(float_str)>1):
j = j+1
if peakPPM = 'ee_pinkH1.xpk':
if 5<=float_num<=6.25:
value1 = ('Atom ' + str(j) + ' ' + str(float_str[0]) + ' ' + str(float_str[1]) + '\n')
atom_lines.insert(-1,value1)
tclust_atom = open("tclust.txt","a")
for value1 in atom_lines:
tclust_atom.write(value1)
tclust_atom.close()
只需使用in
在添加字典之前檢查字典中是否已存在某個鍵。
dict = {}
for line in atomName.readlines()
atom_name = line.split()[1][1:-1]
if (atom_name in dict):
atom_value = float(line.split()[2])
dict[atom_name] = atom_value
由於看起來您要檢查每一行有多個鍵值對,因此可以在每一行中重復執行該函數,如下所示:
dict = {}
for line in atomName.readlines()
atom_name = line.split()[1][1:-1]
if (atom_name in dict):
atom_value = float(line.split()[2])
dict[atom_name] = atom_value
atom_name = line.split()[8][1:-1]
if (atom_name in dict):
atom_value = float(line.split()[9])
dict[atom_name] = atom_value
您是要編輯這篇文章嗎? 我也回答了您的舊重復帖子。
您可以擴展正則表達式模式以包括化學位移,並在每次比賽中獲得所需的東西。 將括號放在要保留的模式部分周圍,以便將其捕獲。
pattern = '''{(\d\.H\d'?)}\s(\d\.\d+)\s'''
rex = re.compile(pattern)
遍歷所有比賽; 名稱和班次將在match.groups()元組中; 如果尚未看到該名稱,則將其添加到詞典中。
with open(filepath) as atom_name:
data = atom_name.read()
result = {}
for match in rex.finditer(data):
name, shift = match.groups()
#print(name,shift)
if name not in result:
result[name] = float(shift)
如果文件太大而無法一次讀取,則一次提取一行信息。
with open(filepath) as atom_name:
for line in atom_name:
for match in rex.finditer(line):
name, shift = match.groups()
#print(name,shift)
if name not in result:
result[name] = float(shift)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.