如何解决当我在 Python 中尝试使用 tab 或不使用 tab 时,关闭功能不起作用
我一直在努力解决这个简单的问题,但是,我真的不明白为什么总是这样! 我的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",f,a3,b3,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
不是一个文件 - 它是一个字符串。
这是学习有意义的变量命名的好时机。
,我看到的问题如下:
return ReorderableListView(
children: [
for (int index = 0; index < _items.length; index++)
ListTile(
key: Key('$index'),title: Text('Item $index'),),],onReorder: (int oldIndex,int newIndex) {},);
您将 f 的值更改为字符串值。
如果更改该行中变量 f 的名称并将 f 用于文件的解决方案。
,您正在重新分配给 f 变量
(a,f) = line.split("\t")
您需要更改 f
以外的变量名称。
你也可以使用 open 这样你就不需要关闭文件
with open(filename ) as fp:
#your logic
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。