首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python / MDAnalysis SelectionError:选择失败:‘无法将字符串转换为浮动

Python / MDAnalysis SelectionError:选择失败:‘无法将字符串转换为浮动
EN

Stack Overflow用户
提问于 2018-12-19 12:12:58
回答 1查看 149关注 0票数 1

我是Python和MDAnalysis的新手。我正在尝试使用MDAnalysis进行分析。但是,我遇到了以下错误:

SelectionError: Selection failed: could not convert string to float 'cold'.

我想选择"BF4“、"EMI”和"TMA“的分子。选择取决于分子的z位置。我有coldcnew,但是我不能将每个分子的z坐标的值与这两个值进行比较,因为它们被认为是字符串而不是浮点。

你能帮帮我吗?非常感谢

代码语言:javascript
复制
  with MDAnalysis.Writer("tube.xtc",tube.n_atoms) as W: 
     t=50 
     with open('lineCNT16.gro','w') as f: 
        for ts in universe.trajectory: 
           W.write(tube) 
           tube = sum(sorted(tube, key=lambda x: x.position[2]))  
           lobf = [] 
           chunk = 40 
           for i in range(len(tube) // 40):  # how many times can we chunk the tube? 
            piece = tube[i*chunk:(i+1)*chunk]  # this is selecting [0:20] first, then [20:40] etc 
            position = piece.positions.mean(axis=0) 
            lobf.append(position) 

           print (ts,lobf) 
           mol=29627 
           f.write('Generated by trjconv : Ionic liquid simulation t=  '"%s\n"%t) 
           f.write('18\n') 
           f.write('    1C08      C29626   4.247   4.253   7.544\n') 
           cold = 7.544 
           for position in lobf: 
            a=position[0]/10   
            b=position[1]/10 
            c=position[2]/10 
            cnew=c 
            print(cnew) 
            f.write('    1C08      ')     
            f.write('C')   
            f.write("%0.8s"%mol) 
            f.write('   ')      
            f.write("%0.5s"%a)  
            f.write('   ') 
            f.write("%0.5s"%b)   
            f.write('   ') 
            f.write("%0.5s\n"%c) 
            mol=mol+1 
            BF4=universe.atoms.select_atoms("resname BF4 and prop z >= cold and prop z<= cnew") 
            #EMI=universe.atoms.select_atoms("resname EMI and prop z >= cold and prop z<= cnew") 
            #TMA=universe.atoms.select_atoms("resname TMA and prop z >= cold and prop z<= cnew") 
            #print(EMI) 
            #print(BF4) 
            #print(TMA) 
            cold=cnew 
           f.write('    1C08      C29643   4.247   4.253   12.64\n')  
           f.write('6.15000   6.20000  20.18420\n') 
           t=t+50.00000   
           mol=29627 
EN

回答 1

Stack Overflow用户

发布于 2019-01-07 18:21:47

代码语言:javascript
复制
BF4=universe.atoms.select_atoms("resname BF4 and prop z >= cold and prop z<= cnew") 

MDAnalysis期望实际值(浮动,如5.7),而不是字符串(“冷”)。它告诉你它需要一个数字而不是字符串。例如:

代码语言:javascript
复制
BF4=universe.atoms.select_atoms("resname BF4 and prop z >= 5.2 and prop z<= 7.5"

您有一个变量"cold",但是您可能需要学习如何将选择字符串与该变量连接起来的技巧。

下面是一个解释它的小教程:https://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53851027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档