我怎样才能在Python中创建一个if循环来表示“如果我的某一列(在本例中为 10)中存在某些内容(在本例中为 1/1)?
我有一个像这样的文件:
Chr10 31 . T C 35.1 . DP=7;AF1=1;CI95=1,1;DP4=0,0,7,0;MQ=23;FQ=-48 GT:PL:GQ 1/1:68,21,0:39**
Chr10 445 . G T 34 . DP=23;AF1=0.5;CI95=0.5,0.5;DP4=7,5,6,3;MQ=19;FQ=12.3;PV4=1,6.2e-08,1,0.27 GT:PL:GQ 0/1:64,0,39:42
Chr10 447 . A C 93 . DP=26;AF1=1;CI95=1,1;DP4=0,0,13,8;MQ=19;FQ=-90 GT:PL:GQ 1/1:126,63,0:99
Chr10 449 . G C 5.46 . DP=28;AF1=0.4999;CI95=0.5,0.5;DP4=9,4,6,4;MQ=23;FQ=7.8;PV4=0.69,4.3e-08,0.037,1 GT:PL:GQ 0/1:34,0,130:34
Chr10 481 . C T 67 . DP=55;AF1=0.5;CI95=0.5,0.5;DP4=17,22,10,5;MQ=25;FQ=70;PV4=0.22,1.5e-07,1,1 GT:PL:GQ 0/1:97,0,152:99
我想创建一个循环来查找第 10 列中包含 1/1
的所有行。
这是我编写的脚本:
pilup= open ("libary_ts.sorted.pilup", "rb+")
with open ("select.txt", "wb+") as ch:
for data in ch:
e=data.split()
if e[9] == "1/1":
但我知道这是错误的,因为 if e[9] ==
意味着第 10 列是否具有精确的 1/1,正如您在示例数据中看到的那样我说我总是有这样的东西:
1/1:213,60,0:99
或者
0/1:43,0,118:46
我只是想知道我应该使用什么来使这个 if 语句正常工作?有没有什么符号表示包含?我到处都找不到它!提前致谢!
I have a file like this:
Chr10 31 . T C 35.1 . DP=7;AF1=1;CI95=1,1;DP4=0,0,7,0;MQ=23;FQ=-48 GT:PL:GQ 1/1:68,21,0:39**
Chr10 445 . G T 34 . DP=23;AF1=0.5;CI95=0.5,0.5;DP4=7,5,6,3;MQ=19;FQ=12.3;PV4=1,6.2e-08,1,0.27 GT:PL:GQ 0/1:64,0,39:42
Chr10 447 . A C 93 . DP=26;AF1=1;CI95=1,1;DP4=0,0,13,8;MQ=19;FQ=-90 GT:PL:GQ 1/1:126,63,0:99
Chr10 449 . G C 5.46 . DP=28;AF1=0.4999;CI95=0.5,0.5;DP4=9,4,6,4;MQ=23;FQ=7.8;PV4=0.69,4.3e-08,0.037,1 GT:PL:GQ 0/1:34,0,130:34
Chr10 481 . C T 67 . DP=55;AF1=0.5;CI95=0.5,0.5;DP4=17,22,10,5;MQ=25;FQ=70;PV4=0.22,1.5e-07,1,1 GT:PL:GQ 0/1:97,0,152:99
and I would like to make a loop that finds all the lines which have 1/1
in their 10th column.
This is the script that I wrote:
pilup= open ("libary_ts.sorted.pilup", "rb+")
with open ("select.txt", "wb+") as ch:
for data in ch:
e=data.split()
if e[9] == "1/1":
but I know it's wrong since the if e[9] ==
means if the column 10 has the exact 1/1, and as you can see in the sample data that I put I always has somthing like this:
1/1:213,60,0:99
or
0/1:43,0,118:46
I just wanted to know what should I use to make this if statement work properly? Is there any symbol which says contain? I couldn’t find it anywhere! Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它可以出现在列中的任何位置:
如果它必须出现在列的开头,如您的示例所示:
或者,如果列始终包含冒号分隔的值:
最后一个是三者中最严格的,并且将是我的个人喜好。
If it can appear anywhere in the column:
If it must appear at the start of the column as in your example:
Alternatively, if the column always contains colon-separated values:
The last one is the strictest of the three, and would be my personal preference.
首先,
open("select.txt", "wb+")
将删除文件的内容。我假设您正在迭代pilup
。您可以使用 < code>in 运算符 来测试
1/1
是否出现在第十个元素中的任何位置:或者,使用
startswith
检查第十个元素是否,嗯,以“1/1”
开头:First of all,
open ("select.txt", "wb+")
will delete the file's contents. I assume you're iterating overpilup
.You can use the
in
operator to test whether1/1
occurs anywhere in the tenth element:Alternatively, use
startswith
to check whether the tenth element, well, starts with"1/1"
:如果
1/1
始终位于该列的开头,则可以使用startswith
字符串方法:If
1/1
will always be at the start of that column, you can use thestartswith
string method: