从文件中获取特定输出 [英] Getting a certain output from a file

查看:63
本文介绍了从文件中获取特定输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。


我是python的新手,我的生物学专业需要至少参加一门生物信息学课程。


我教授给的作业有些困难。作业如下:


编写一个Perl脚本,计算DNA序列的补码

。换句话说,你的脚本应该转换所有

A'到T',C'到G',G'到C'和T'是'A'的。


输入是一个名为dna.txt的文件中FASTA格式的序列。


例如,如果文件包含


>人物

ACCGT


然后程序的输出应该是TGGCA。请注意,您的程序应该以

的格式运行,而不仅仅是给定的示例。


________________________


我该怎么做,因为我完全不知道。他想要一个非常基本的代码。


此外,Python的在线教程在哪里?我确实订了一本书,但在收到之前我需要一些东西帮我做作业。谢谢,我真的很感激。

Hi, all.

I''m very new to python and it''s a requirement for my biology major to take at least one class in bioinformatics.

I am having some difficulty with the homework my professor gave. The assignment is below:

Write a Perl script that computes the complement
of a DNA sequence. In other words, your script should convert all
A''s to T''s, C''s to G''s, G''s to C''s, and T''s to A''s.

The input is one sequence in FASTA format in a file called "dna.txt".

For example if the file contains

>human
ACCGT

then the output of the program should be TGGCA. Note that your program should work for
any sequence in this format and not just the given example.

________________________

How do I do this, because I have absolutely no idea. He wants a very basic code.

Also, where are good tutorials online for Python? I did order a book but until I receive it I need some thing to help me with homework. Thank you, I really appreciate it.

推荐答案

你的问题是关于Perl还是Python?在Python中,您将需要使用 str 方法 replace()。示例:
Is your question about Perl or Python? In Python, you will want to use str method replace(). Example:
展开 | 选择 | Wrap | 行号


它是Python。我的大学在本学期改为使用python。


所以我尝试了你的建议并进行了搜索。我尝试以我发现的方式执行以下操作:


with open(" c:/temp/dna.fasta.txt"," r")as myfile :

lines = myfile.readlines()

print(lines)

[''> human \ n'','' ACCGT \ n'']


str =" ACCGT";

>>> print str.replace(" A"," T")

语法错误:语法无效


^^这种方式给了我一个错误&突出显示str


然后我尝试了这样做:


" ACCGT" .replace(" A"," T" ;)

''TCCGT''


^这有效,但它必须为每个字母做这个,所以我试过这个:



ACCGT.replace(A,T+C,G+T,A+ ; G"," C")


Traceback(最近一次调用最后一次):

文件"< pyshell#12>",行1,在< module>

" ACCGT" .replace(" A"," T"" C"," G"" T"," A" ; +G,C)

TypeError:replace()最多需要3个参数(给定5个)



如何在不复制最后一个输出然后再制作新的str.replace行的情况下,如何使用代码一次替换所有字母?
It''s Python. My university changed to using python this semester.

So I tried what you suggested and did a search. I tried to do it one way I found which was the following:

with open ("c:/temp/dna.fasta.txt", "r") as myfile:
lines = myfile.readlines()
print(lines)
[''>human \n'', ''ACCGT \n'']

str = "ACCGT";
>>> print str.replace("A", "T")
SyntaxError: invalid syntax

^^ that way gave me an error & highlighted str

Then I tried your way by doing this:

"ACCGT".replace("A", "T")
''TCCGT''

^ This worked but it has to do it for each of the letters so I tried this:


"ACCGT".replace ("A", "T" + "C", "G" + "T", "A" + "G", "C")

Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
"ACCGT".replace ("A", "T" + "C", "G" + "T", "A" + "G", "C")
TypeError: replace() takes at most 3 arguments (5 given)


How can I use a code to do replace all the letters at once without each time copying the last output and then making a new str.replace line?


不要将 str 用作变量名,因为内置函数 str()将被屏蔽。发布代码时请使用代码标签


要替换多个字母:
Do not use str as a variable name, because the built-in function str() will be masked. Please use code tags when posting code.

To replace multiple letters:
展开 | 选择 | Wrap | 行号


这篇关于从文件中获取特定输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆