【问题标题】:AttributeError: 'str' object has no attribute 'readlines'AttributeError:“str”对象没有属性“readlines”
【发布时间】:2014-11-25 21:45:34
【问题描述】:

我的代码有问题。 我收到了这个 AttributeError ,我不知道为什么。 请有人提供一点见解,谢谢!!! 这是用python 3编写的, 我正在尝试制作图表。

import sys
data = {}
def main():
    filename = sys.argv[1]
    parseFile(filename)
    function()    
def parseFile(fn):
    print("Parsing", fn)
    infile = open(fn, "r")
    for line in infile:
        line = line[:-1]
        tokens = line.split()
        print(tokens)
        if line[0]=="#":
            line.readline() #<-- this is my problem line
        rsid = (tokens[0])
        genotype = (tokens[3])
        data[rsid] = genotype
    infile.close()
main()

# This data file generated by 23andMe at: Wed Jan 26 05:37:08 2011
#
# Below is a text version of your data. Fields are TAB-separated
# Each line corresponds to a single SNP.  For each SNP, we provide its identifier 
# (an rsid or an internal id), its location on the reference human genome, and the 
# genotype call oriented with respect to the plus strand on the human reference 
# sequence.     We are using reference human assembly build 36.  Note that it is possible 
# that data downloaded at different times may be different due to ongoing improvements 
# in our ability to call genotypes. More information about these changes can be found at:
# https://www.23andme.com/you/download/revisions/
# 
# More information on reference human assembly build 36:
# http://www.ncbi.nlm.nih.gov/projects/mapview/map_search.cgi?taxid=9606&build=36
#
# rsid  chromosome  position    genotype
rs4477212   1   72017   AA
rs3094315   1   742429  AA
rs1799883   1   742429  AA
rs3131972   1   742584  GG
rs12124819  1   766409  AA
rs11240777  1   788822  GG
rs6681049   1   789870  CC
rs4970383   1   828418  CC
rs4475691   1   836671  CC
rs7537756   1   844113  AA

【问题讨论】:

  • 那是什么语言?哪一行的确切错误是什么?你试过减少你的例子吗?
  • 啊。对不起。 Python。错误在 line.readline() 我试图减少我的例子,但不是最好的这样做
  • 为什么你有 line.readline() 那里?你的意图是什么?
  • 我也在尝试这个 print(tokens) line = line.readline()[1:] rsid = (tokens[0]) 但无济于事
  • 你能举一个你正在阅读的文件中的一些行的例子吗?

标签: string python-3.x attributeerror


【解决方案1】:

属性错误表示readline不是字符串方法。

在这个sn-p中:

for line in infile:
    line = line[:-1]
    tokens = line.split()

我猜想(对吗?)那 line[:-1] 是去掉换行符。如果是这种情况,请尝试以下方法:

for line in infile:
    line = line.strip()
    tokens = line.split()

strip 将去掉换行符和回车符。并且由于您刚刚将行更改为没有换行符的文本,因此您可以使用 line.readline() 删除您的行。

更新: 跳过以#开头的行

for line in infile:
    line = line.strip()
    if line[0]=="#":
        continue
    tokens = line.split()

“跳过”是指您想忽略它们。

此外,为了获得良好的格式,您应该在 parseFile 函数中包含以下行:

def parseFile(fn):
    global data
    ...

【讨论】:

  • 好的。我试图跳过#。我刚刚更新了上面的代码^^^以提供更多信息。 line.strip() 命令还能用吗?
  • 在分割前不需要去除行尾的空格。 ' 1 2 4 '.split() == ['1', '2', '4'].
  • @Terry 你是对的,如果你在空格上分割。无论我分割什么(很少是空格),我总是养成使用条带的习惯。
  • @brunodesthuilliers 如果他询问解析文本,这家伙显然才刚刚开始。一步一步,明白我的意思。你应该把它调低一点。
  • @user1269942 我完全理解 OP 是初学者,这就是为什么您不应该将全局变量呈现为“良好形式”而是向他展示如何避免它们的确切原因。从一开始就学习正确的做法比学习坏习惯然后尝试改掉它们更容易。
猜你喜欢
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 2021-10-04
  • 2019-12-02
  • 2021-09-25
  • 2014-03-04
相关资源
最近更新 更多