String ';和'';或';对于python,字符串之间的语句不相等
我正在用python阅读一个文本文件。如果第一个字符不等于String ';和'';或';对于python,字符串之间的语句不相等,string,python-3.x,equals-operator,String,Python 3.x,Equals Operator,我正在用python阅读一个文本文件。如果第一个字符不等于#或@,则将读取每一行。文本的内容粘贴在最后 这项工作: if line[0] != '#' and line[0] != '@': value=float(line[3:8]) 但这不起作用: if line[0] != ('#' and '@'): value=float(line[3:8]) ValueError: could not convert string to float: 'his f' if line[0
#
或@
,则将读取每一行。文本的内容粘贴在最后
这项工作:
if line[0] != '#' and line[0] != '@':
value=float(line[3:8])
但这不起作用:
if line[0] != ('#' and '@'):
value=float(line[3:8])
ValueError: could not convert string to float: 'his f'
if line[0] != ('#' or '@'):
value=float(line[3:8])
ValueError: could not convert string to float: ' tit'
这也不起作用:
if line[0] != ('#' and '@'):
value=float(line[3:8])
ValueError: could not convert string to float: 'his f'
if line[0] != ('#' or '@'):
value=float(line[3:8])
ValueError: could not convert string to float: ' tit'
那么为什么或
和和
都不起作用呢
文本文件的内容:
# This file was created Wed Feb 21 12:32:25 2018
# Created by:
# :-) GROMACS - gmx do_dssp, VERSION 5.1.1 (-:
#
# Executable: /shared/ucl/apps/gromacs/5.1.1/intel-2015-update2/bin//gmx
# Data prefix: /shared/ucl/apps/gromacs/5.1.1/intel-2015-update2
# Command line:
# gmx do_dssp -f md_0_1_noPBC.xtc -s md_0_1.tpr -ssdump ssdump.dat -map ss.map -o ss.xpm -sc scount.xvg -a area.xpm -ta totarea.xvg -aa averarea.xvg -tu ns
# gmx do_dssp is part of G R O M A C S:
#
# Gnomes, ROck Monsters And Chili Sauce
#
@ title "Secondary Structure"
@ xaxis label "Time (ns)"
@ yaxis label "Number of Residues"
@TYPE xy
@ subtitle "Structure = A-Helix + B-Sheet + B-Bridge + Turn"
@ view 0.15, 0.15, 0.75, 0.85
@ legend on
@ legend box on
@ legend loctype view
@ legend 0.78, 0.8
@ legend length 2
@ s0 legend "Structure"
@ s1 legend "Coil"
@ s2 legend "B-Sheet"
@ s3 legend "B-Bridge"
@ s4 legend "Bend"
@ s5 legend "Turn"
@ s6 legend "A-Helix"
@ s7 legend "3-Helix"
@ s8 legend "5-Helix"
0 278 106 199 10 44 60 9 15 0
0.1 267 118 203 7 52 47 10 6 0
0.2 245 144 175 8 54 51 11 0 0
0.3 264 118 192 8 58 56 8 3 0
第[0]行不在['#','@']
将起作用
它检查给定数组中是否存在
行[0]
谢谢,但是你知道为什么或和和不起作用吗?据我们所知,这只是python的无效语法。在您的第二个示例中,“#”和“@”
根本不是一个值,我认为或和和仅用于语句。(“#”和“@”)
是一个计算结果为@
的表达式。这是因为Python中的操作和和以及或返回确定逻辑运算结果的第一个操作数,如果第一个操作数为False
,则返回第一个操作数,否则返回第二个操作数和。在逻辑上下文中,所有不等于null字符串的字符串都计为True
,因此和将始终返回第二个字符串,而或将始终返回第一个字符串。