Subscribe
提醒
guest

17 评论
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
12
12
6 年 之前

works[name] = works[name] + 1
字典里没有的关键字不是会出错吗?
为什么我们的代码可以这样给字典赋值

实例
#!/usr/bin/python3

dict = {‘Name’: ‘Runoob’, ‘Age’: 7, ‘Class’: ‘First’}

print (“dict[‘Alice’]: “, dict[‘Alice’])
以上实例输出结果:

Traceback (most recent call last):
File “test.py”, line 5, in
print (“dict[‘Alice’]: “, dict[‘Alice’])
KeyError: ‘Alice’

12
12
Reply to  tom
6 年 之前

works开始不是一个空字典,我调试看了第一个name是张三
works[张三] = works[张三] + 1,为什么这个works[张三] 不报错,实在有点搞不清

12
12
6 年 之前

>>> dict[‘a’]=dict[‘a’]+2
Traceback (most recent call last):
File “”, line 1, in
KeyError: ‘a’有点搞不懂

12
12
6 年 之前

works开始不是一个空字典,我调试看了第一个name是张三
works[张三] = works[张三] + 1,为什么这个works[张三] 不报错,实在有点搞不清

12
12
Reply to  tom
6 年 之前

好的

PY GISER7
PY GISER7
6 年 之前

File “C:/Users/Administrator/PycharmProjects/excel/main.py”, line 19, in
x, y = findNeiYeRenYuan(worksheet)
TypeError: cannot unpack non-iterable NoneType object

百度说是:解决方法:报错的原因是函数返回值得数量不一致,查看函数返回值数量和调用函数时接收返回值的数量是不是一致,修改一致即可。但是我找了半天也看不出。

def findNeiYeRenYuan(sheet):
for x in range(sheet.nrows):
for y in range(sheet.ncols):
if sheet.cell_value(x, y) == “内页人员”:
return x, y

for root, dirs, files in os.walk(‘C:\\Users\\Administrator\\PycharmProjects\\EXCEL样例数据’):
for file in files:
filename = os.path.join(root, file)
workbook = xlrd.open_workbook(filename)
worksheet = workbook.sheet_by_index(0)
x, y = findNeiYeRenYuan(worksheet)

PY GISER7
PY GISER7
Reply to  tom
6 年 之前

C:\Users\Administrator\PycharmProjects\EXCEL1\venv\Scripts\python.exe “C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.1\plugins\python-ce\helpers\pydev\pydevd.py” –multiproc –qt-support=auto –client 127.0.0.1 –port 51725 –file C:/Users/Administrator/PycharmProjects/EXCEL1/main1.py
pydev debugger: process 9968 is connecting

Connected to pydev debugger (build 193.5662.61)
Traceback (most recent call last):
File “C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.1\plugins\python-ce\helpers\pydev\pydevd.py”, line 1434, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File “C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+”\n”, file, ‘exec’), glob, loc)
File “C:/Users/Administrator/PycharmProjects/EXCEL1/main1.py”, line 19, in
x, y = findNeiYeRenYuan(worksheet)
TypeError: cannot unpack non-iterable NoneType object

PY GISER59
PY GISER59
6 年 之前

C:\Users\Administrator\Desktop\test1\venv\Scripts\python.exe “C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.1\plugins\python-ce\helpers\pydev\pydevd.py” –multiproc –qt-support=auto –client 127.0.0.1 –port 51850 –file C:/Users/Administrator/Desktop/test1/venv/main.py
pydev debugger: process 9304 is connecting

Connected to pydev debugger (build 193.5662.61)
Traceback (most recent call last):
File “C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.1\plugins\python-ce\helpers\pydev\pydevd.py”, line 1434, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File “C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py”, line 18, in execfile
exec(compile(contents+”\n”, file, ‘exec’), glob, loc)
File “C:/Users/Administrator/Desktop/test1/venv/main.py”, line 8
for x in range(worksheet.nrows)
^
SyntaxError: invalid syntax

Process finished with exit code -1

PY GISER37
PY GISER37
6 年 之前

import xlrd
import os

r = input(‘请输入excle表格所在文件夹:’)
t = input(‘请输入需要统计字段:’)
w = input(‘请输入统计结果保存路径:’)
wname = input(‘请输入统计结果文件名称:’)

# 定义findtext函数,通过数据表来获取“内业人员”所在的行列号
def findtext(sheet):
for x in range(sheet.nrows):
for y in range(sheet.ncols):
if sheet.cell_value(x, y) == t:
return x, y
return ‘Error’

# 定义writeToTxt函数,将结果写入TXT文件
def writeToTxt(wks):
f = open(“%s\\%s.txt” % (w, wname), ‘w’)
for key in wks.keys():
f.write(“%s一共工作了%s次\r” % (key, wks[key]))
for errorname in errortxt:
f.write(‘错误信息:\r表:%s 未找到输入“%s”字段\r’ % (errorname, t))

works = {} # {人名,工作次数}{key,value}
errortxt = {}
for root, dirs, files in os.walk(r): # 获取excle文件目录及文件名 ‘C:\GIS Python第一季\excle\EXCEL样例数据’
for file in files:
filename = os.path.join(root, file) # 拼接文件目录及文件名为完整文件路径
workbook = xlrd.open_workbook(filename) # 使用open_workbook函数打开excle文件
sheet = workbook.sheet_by_index(0) # 使用sheet_by_index函数定义需要操作excle文件的第几个sheet页
if findtext(sheet) == ‘Error’:
# print(‘%s表:未找到输入统计字段’ % filename)
errortxt[file] = 1
else:
x, y = findtext(sheet) # 获得内业人员所在的行列
for i in range(x + 1, sheet.nrows): # 在内业人员所在行+1和总行数之间i循环
name = sheet.cell_value(i, y) # 获取i行y列值
if name != “” and name != t: # 排除内业人员及空值
if name in works.keys(): # 判断name是否在works字典的keys里面
works[name] = works[name] + 1 # 如果名字在works字典的keys里面,那么名字所对应的value加一
# works[name] = works[name] + 1这个语句是个赋值语句,就是把一个key所对应的value加一
else:
works[name] = 1 # 如果名字不在works字典的keys里,那么把名字添加到works字典里,并把相应的value赋值1
# 字典如果key一样的话,那么执行赋值works[name],
writeToTxt(works) # 把字典的数据写入txt文档

混世魔王
混世魔王
5 年 之前

一年后再次复习xlrd,总结了两个问题,供其他人参考:
1,xlrd目前为2.0版本,升级后不支持xlsx文件。针对本课实验,可以卸载xlrd(pip uninstall xlrd),再安装低版本(pip install xlrd==1.2.0)。
2,再pycharm中新建*.py文件时,不能用xlrd.py为文件名,会导致open_workbook方法不可用。

17
0
交流思想,留下评论.x