【问题标题】:Error: 'set' object has no attribute.. - Python3错误:'set' 对象没有属性.. - Python3
【发布时间】:2016-04-15 22:16:10
【问题描述】:

我正在开发一个程序,该程序需要从 pandas 中获取我在表中的行,并将每一行存储为字典条目,其中一列作为键,其他存储在具有该键的对象中。它似乎工作正常,当它循环将它们添加到字典时我没有收到任何错误,但是如果我尝试使用 SeqDict['key'].classattribute 之类的语法从该字典中调用特定项目,我会收到一个错误说“'set'对象没有属性'classattribute'。我使用了错误的语法还是我的代码有其他问题?这是我第一次尝试编程,如果我犯了一个愚蠢的错误或我的代码写得很低效。

我的代码的前半部分似乎工作正常

import os
import pandas as pd

print("Please move your CSV to the desktop before continuing.")
csvfilename = input("Enter the name of your CSV file: ")
desktopfilepath = os.path.expanduser("~/Desktop/")
csvfilepath = desktopfilepath + csvfilename

seqtable = (pd.read_csv(csvfilepath, 
                    dtype=str, 
                    header=0, 
                    usecols=["Well", "Core Sample ID", "Sample ID", "Exon"]))

newseqtable = pd.DataFrame.dropna(seqtable)
print(newseqtable)

y = len(newseqtable.index)
print(y, "files to rename.") 

我假设这部分是我做错的地方

class SeqID:
    def __init__(self,well,sequence,exon):
        self.well = well
        self.sequence = sequence
        self.exon = exon
SeqDict = {}

x = 0
for x in range(y):
    ID = newseqtable.iat[x,1]
    well = newseqtable.iat[x,0]
    sequence = newseqtable.iat[x,2]
    exon = newseqtable.iat[x,3]
#    print(ID + "," + well + "," + sequence + "," + exon + ".") #This works fine when I choose to run it
    SeqDict[ID] = {SeqID(well,sequence,exon)}
SeqDict  #This prints the dictionary keys I would expect

SeqDict['key'].well  # This returns the error when I insert a key

谢谢

【问题讨论】:

    标签: class python-3.x dictionary pandas


    【解决方案1】:

    这一行:

    SeqDict[ID] = {SeqID(well,sequence,exon)}
    

    创建一个 1-set。您可能的意思是:

    SeqDict[ID] = SeqID(well,sequence,exon)
    

    【讨论】:

    • 这解决了我的问题。非常感谢。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 2018-03-11
    相关资源
    最近更新 更多