gpt4 book ai didi

python - 一次读取4行

转载 作者:太空宇宙 更新时间:2023-11-03 18:53:15 25 4
gpt4 key购买 nike

我正在尝试一次读取 fastq 文件四行。文件中有几行。但是当我输入代码时,我得到了这个:

Traceback (most recent call last):

File "fastq.py", line 11, in

line1 = fastq_file.readline()

AttributeError: 'str' object has no attribute 'readline'

这是我的代码:

import Tkinter, tkFileDialog #asks user to select a file

root = Tkinter.Tk()
root.withdraw()

fastq_file = tkFileDialog.askopenfilename()

if fastq_file.endswith('.fastq'): #check the file extension
minq = raw_input("What is your minimum Q value? It must be a numerical value.") #receives the minimum Q value
while True:
line1 = fastq_file.readline()
if not line1:break
line2 = fastq_file.readline(2)
line3 = fastq_file.readline(3)
line4 = fastq_file.readline(4)

txt = open(practice.text)
txt.write(line1) #puts the lines into the file
txt.write("\n")
txt.write(line2)
txt.write("\n")
txt.write(line3)
txt.write("\n")
txt.write(line4)
txt.write("\n")
print "Your task is complete!"

else:
print "The file format is not compatible with the FastQ reader program. Please check the file and try again."

我该如何解决这个问题,以便我可以将每一行分配给一个字符串,然后将这些字符串写入文本文件中?

最佳答案

您需要先打开文件

while True:
with open(fastq_file) as fastq_file_open:
line1 = fastq_file_open.readline()

您可能想在实际进入 while 循环之前打开它们,但我没有其余的代码,所以我无法准确地构造它。

关于python - 一次读取4行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17822427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com