帮我改错一下 import json import pandas as pd import requests data1 = pd.read_excel('C:\\Users\\Administrator\\PycharmProjects\\pythonProject10\\用户信息.xls') head = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0', 'cookie': 'UM_distinctid=17fed3bf7296d4-01aeae5d80942c-1f343371-1fa400-17fed3bf72a1163; __client_id=2ec00f15870204623a78bf6f63f7c99a85774791; CNZZDATA5476811=cnzz_eid%3D1690978113-1633007100-https%253A\%252F\%252Fcn.bing.com\%252F%26ntime%3D1651749421; _uid=0; login_referer=https%3A\%2F\%2Fwww.luogu.com.cn\%2Fuser\%2F387021' } data = requests.get("https://www.luogu.com.cn/fe/api/contest/scoreboard/68651?page=1", headers=head) data.encoding = 'utf-8' json_object = json.loads(data.text) g = dict() rank = dict() ID = {'U197159': 'A', 'U196210': 'B', 'U215461': 'C', 'U197241': 'D', 'U197281': 'E', 'U215474': 'F', 'U197124': 'G', 'U215470': 'H'} idx = 1 for i in json_object['scoreboard']['result']: g[i['user']['name']] = [] rank[i['user']['name']] = idx idx = idx + 1 for j in i['details']: if i['details'][j]['score'] >= 0: g[i['user']['name']].append(ID[j]) for i in range(33): if data1.loc[i, '洛谷账号'] in rank.keys(): data1.loc[i, '排名'] = rank[data1.loc[i, '洛谷账号']] for j in g[data1.loc[i, '洛谷账号']]: data1.loc[i, j] = 'AC' else: data1.loc[i, '排名'] = -1 data1.to_excel('C:\\Users\\Administrator\\PycharmProjects\\pythonProject10\\result.xls', index=0)
时间: 2023-09-27 16:06:25 浏览: 125
操作模式-详解pandas库pd.read_excel操作读取excel文件参数整理与实例
这里主要是第23行和第37行的问题。
第23行中,字典 ID 的定义语句没有结束,应该在后面添加一个右括号。
第37行中,to_excel 方法的 index 参数应该是 False 而不是数字 0。
修改过后的代码如下:
```python
import json
import pandas as pd
import requests
data1 = pd.read_excel('C:\\Users\\Administrator\\PycharmProjects\\pythonProject10\\用户信息.xls')
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0',
'cookie': 'UM_distinctid=17fed3bf7296d4-01aeae5d80942c-1f343371-1fa400-17fed3bf72a1163; __client_id=2ec00f15870204623a78bf6f63f7c99a85774791; CNZZDATA5476811=cnzz_eid%3D1690978113-1633007100-https%253A\%252F\%252Fcn.bing.com\%252F%26ntime%3D1651749421; _uid=0; login_referer=https%3A\%2F\%2Fwww.luogu.com.cn\%2Fuser\%2F387021'
}
data = requests.get("https://www.luogu.com.cn/fe/api/contest/scoreboard/68651?page=1", headers=head)
data.encoding = 'utf-8'
json_object = json.loads(data.text)
g = dict()
rank = dict()
ID = {'U197159': 'A', 'U196210': 'B', 'U215461': 'C', 'U197241': 'D', 'U197281': 'E', 'U215474': 'F', 'U197124': 'G', 'U215470': 'H'}
idx = 1
for i in json_object['scoreboard']['result']:
g[i['user']['name']] = []
rank[i['user']['name']] = idx
idx = idx + 1
for j in i['details']:
if i['details'][j]['score'] >= 0:
g[i['user']['name']].append(ID[j])
for i in range(33):
if data1.loc[i, '洛谷账号'] in rank.keys():
data1.loc[i, '排名'] = rank[data1.loc[i, '洛谷账号']]
for j in g[data1.loc[i, '洛谷账号']]:
data1.loc[i, j] = 'AC'
else:
data1.loc[i, '排名'] = -1
data1.to_excel('C:\\Users\\Administrator\\PycharmProjects\\pythonProject10\\result.xls', index=False)
```
希望这个修改能够帮助到你。
阅读全文