当我在 Python 中尝试使用制表符或不使用制表符时,关闭功能不起作用
Close fuction dosen´t work when I try with tab or not in Python
我一直在尝试解决这个简单的问题,但是,我真的不明白为什么总是这样!
我的 python 脚本:
import re
#### PART 1
#### FIRST: we create a dictionary (an analog of a perl hash)
#### using the file containing two columns= IDS and labels
fileSet = {} ## empty dictionary
fb = open('NT_ID_S.csv', 'r')
for line2 in fb:
if not line2: break
line2 = line2.rstrip("\n")
(ax, bx) = line2.split(";")
fileSet[ax] = bx
fb.close()
#### PART 2
#### NOW, we will open our main file and apply while loops to
#### search in parts of every line (nested loops)
f = open('S_dN_dS_Tajima.tsv', 'r')
for line in f: # For main file (with 6 columns)
if not line: break
line = line.rstrip("\n")
(a, b, c, d, e, f) = line.split("\t")
if (a == "ID1"): continue ### continue is like "next" in perl; it omits the first line (column names)
if (a == b): continue ### continue is like "next" in perl; it omits lines where the two IDs are the same
#### Defining empty variables for the future new labels
a3 = None
b3 = None
#### now, we will use the same FOR loop to obtain the value
#### for the labels for the first and second IDs
for key, value in fileSet.items():
if (a == key):
a3 = value
elif (b == key):
b3 = value
#### Printing the final line with the new labels
print (a, "\t", b, "\t", c, "\t", d,"\t", e, "\t", f, "\t", a3 , "\t", b3, "\t", end="\n")
f.close()
错误(在脚本的第二部分):
File "merge_two_files_JP_v2.py", line 41, in <module>
f.close()
AttributeError: 'str' object has no attribute 'close'
我知道问题出在“f.close”,首先我尝试更改制表符位置但发生了同样的错误,然后我使用制表符位置,但同样的事情再次发生。
我真的不明白为什么不起作用。
您在这里重新分配 f
:
(a, b, c, d, e, f) = line.split("\t")
所以在这一点之后 f
不是一个文件 - 它是一个字符串。
这是学习有意义的变量命名的好时机。
我看到的问题如下:
(a, b, c, d, e, f) = line.split("\t")
您将 f 的值更改为字符串值。
更改该行中变量 f 的名称并将 f 用于文件的解决方案。
您正在重新分配给 f 变量
(a, b, c, d, e, f) = line.split("\t")
您需要更改 f
以外的变量名称。
您还可以使用 with open 这样您就不需要关闭文件
with open(filename ) as fp:
#your logic
我一直在尝试解决这个简单的问题,但是,我真的不明白为什么总是这样! 我的 python 脚本:
import re
#### PART 1
#### FIRST: we create a dictionary (an analog of a perl hash)
#### using the file containing two columns= IDS and labels
fileSet = {} ## empty dictionary
fb = open('NT_ID_S.csv', 'r')
for line2 in fb:
if not line2: break
line2 = line2.rstrip("\n")
(ax, bx) = line2.split(";")
fileSet[ax] = bx
fb.close()
#### PART 2
#### NOW, we will open our main file and apply while loops to
#### search in parts of every line (nested loops)
f = open('S_dN_dS_Tajima.tsv', 'r')
for line in f: # For main file (with 6 columns)
if not line: break
line = line.rstrip("\n")
(a, b, c, d, e, f) = line.split("\t")
if (a == "ID1"): continue ### continue is like "next" in perl; it omits the first line (column names)
if (a == b): continue ### continue is like "next" in perl; it omits lines where the two IDs are the same
#### Defining empty variables for the future new labels
a3 = None
b3 = None
#### now, we will use the same FOR loop to obtain the value
#### for the labels for the first and second IDs
for key, value in fileSet.items():
if (a == key):
a3 = value
elif (b == key):
b3 = value
#### Printing the final line with the new labels
print (a, "\t", b, "\t", c, "\t", d,"\t", e, "\t", f, "\t", a3 , "\t", b3, "\t", end="\n")
f.close()
错误(在脚本的第二部分):
File "merge_two_files_JP_v2.py", line 41, in <module>
f.close()
AttributeError: 'str' object has no attribute 'close'
我知道问题出在“f.close”,首先我尝试更改制表符位置但发生了同样的错误,然后我使用制表符位置,但同样的事情再次发生。 我真的不明白为什么不起作用。
您在这里重新分配 f
:
(a, b, c, d, e, f) = line.split("\t")
所以在这一点之后 f
不是一个文件 - 它是一个字符串。
这是学习有意义的变量命名的好时机。
我看到的问题如下:
(a, b, c, d, e, f) = line.split("\t")
您将 f 的值更改为字符串值。
更改该行中变量 f 的名称并将 f 用于文件的解决方案。
您正在重新分配给 f 变量
(a, b, c, d, e, f) = line.split("\t")
您需要更改 f
以外的变量名称。
您还可以使用 with open 这样您就不需要关闭文件
with open(filename ) as fp:
#your logic