我正在处理一个需要从pandas中获取表中的行的程序,并将每个行存储为一个字典条目,其中一列作为键,其他存储在具有该键的对象中。它似乎工作正常,当它循环将它们添加到字典时我没有错误,但是如果我尝试使用像SeqDict [' key']这样的语法来调用该字典中的特定项目.class属性,我收到错误说"'设置'对象没有属性' 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
由于
答案 0 :(得分:2)
这一行:
SeqDict[ID] = {SeqID(well,sequence,exon)}
创建一组。你可能意味着:
SeqDict[ID] = SeqID(well,sequence,exon)