所以我试图获得此输出
No Column Sum
0 Company 28
1 Booth 28
2 Full-Time 25
3 Full-Time Visa Sponsor 5
4 Part-Time 1
5 Internship 18
6 Freshman 7
7 Sophomore 9
8 Junior 17
9 Senior 24
10 Post-Bacs 17
11 MS 17
12 PhD 6
13 Alumni 15
但是,我收到此错误
TypeError:元组索引必须是整数或切片,而不是str
我的代码如下
data_employer = {'No': ('Column', 'Sum')}
for vari in range(14):
sum = 0
for row in ReadyColumn:
if row[vari] != '':
sum =+1
data_employer = (companies[vari], sum)
for num in data_employer:
print(num, data_employer [num][0], data_employer[num][1])
outData = []
是因为我不需要调用num而是行号吗?我认为它可以这样工作。任何输入将不胜感激。谢谢!
Traceback (most recent call last):
File "/Prj3Assignt3.py", line 42, in <module>
print(num, data_employer[num][0], data_employer[num][1])
TypeError: tuple indices must be integers or slices, not str
添加了错误
import csv
filename = "CFSpring2018Employers.csv" #I assigned the file to a variable
f = open(filename) #I couldn't leave it default due to UTF-8 error from orginial
reader = csv.reader(f)
f.close
#Dictionary for part 1 'Companies'
companies = {}
countComp = 0 #Sets the 'rows to 0
for row in reader: #reader has assigned value to read csv info
if row[0] == 'Company': #Searches row called 'Company'
for item in row:
companies[countComp] = item #Everytime it shows it puts it into the dictionary
countComp += 1
break
for i in companies: #Allows me to print numbers next to the companies dict
print(i, companies[i])
ReadyColumn = [] #Making a new list for cleaned up data
NumCow = 0
for row in reader:
NumCow +=1
if NumCow > 0 and NumCow < 31 and row[0] != '': #This will read the rows between 0-31, and if the row contains 'Nothing' it skips
ReadyColumn.append(row) #Updates the list
rowNum = -1 #Resets the counter
for row in ReadyColumn:
rowNum += 1
print(rowNum, ','.join(row)) #Joins any weird marks and prints as desired output
data_employer = {'No', 'Column', 'Sum'}
for vari in range(14):
sum = 0
for row in ReadyColumn:
if row[vari] != '':
sum =+1
cleaner_employer = (companies[vari], sum)
for i in data_employer:
print(i, data_employer,i[0], data_employer,i[1])
所以这是我写的代码,以帮助实现这一点。我正在阅读没有 PANDAS 的csv文件(因为我不能将它们用于项目)
答案 0 :(得分:1)
您将函数中间的data_employer
重新定义为元组:data_employer = (companies[vari], sum)
。使用其他名称来避免这种情况。
在以下代码中:
for num in data_employer:
print(num, data_employer [num][0], data_employer[num][1])
num将首先为companies[vari]
,然后为sum
。 companies[vari]
可能是一个字符串。元组只能使用整数访问。 dict可以通过密钥访问(任何可哈希的对象)。
答案 1 :(得分:1)
TL; DR:您正在覆盖变量名称并更改其数据类型。
你用元组覆盖了你的字典!让我们来看看......
首先,您的代码包含注释:
data_employer = {'No': ('Column', 'Sum')} # original dictionary
for vari in range(14):
sum = 0
for row in ReadyColumn: # no idea what ReadyColumn is...post this?
if row[vari] != '':
sum =+1
data_employer = (companies[vari], sum) # the issue occurs here!!!
for num in data_employer:
print(num, data_employer [num][0], data_employer[num][1]) # where you get TypeError
outData = []
显然省略了代码(显示ReadyColumn
和companies
;两者似乎都是某种形式的迭代);但我们仍然可以解决这个问题。
首先,您声明并定义一个名为data_employer
的字典:
data_employer = {'No': ('Column', 'Sum')}
然而,在你的循环中,你最终用元组替换了这个名字!
data_employer = (companies[vari], sum)
这样想:
>>> data_employer = {'No': ('Column', 'Sum')}
>>> isinstance(data_employer, dict)
True
>>> data_employer = (companies[vari], sum)
>>> isinstance(data_employer, dict)
False
>>> isinstance(data_employer, tuple)
True
当您尝试通过[num]
访问它时,它已经从密钥访问(通常是字典)更改为索引访问(通常是元组)。