av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python - KeyError: ’page’這個(gè)錯(cuò)誤要怎么搞?

瀏覽:121日期:2022-07-01 15:22:05

問(wèn)題描述

def parse2csv(filepath): logfile = open(filepath,’r’) source_ip_dict={} res_url_dict={} from_url_dict={} category_dict={} print(’start.....’) for line in logfile:line=line.strip()if line!='': source_ip = line.split(’- -’)[0].strip() if re.match(’[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}’,source_ip):if source_ip_dict.get(source_ip,’-’)==’-’: source_ip_dict[source_ip]=1else: source_ip_dict[source_ip]=source_ip_dict[source_ip]+1 reg=’'[GETPUOSHADINS]{3,8} /’ url_start = re.compile(reg) re_result = url_start.findall(line) if len(re_result)>=1:res_url = ’/’+line.split(re_result[0])[1].split(’ ’)[0]category = strip_detail(res_url.split(’/’))if len(category)>=1: if category[0] in [’article’,’item’,’question’,’topic’,’people’,’celebrity’,’brand’,’page’,’category’]:if category_dict.get(category[0],’-’)==’-’: category_dict[category[0]]=1else: category_dict[category[0]]=category_dict[category[0]]+1if res_url.endswith(’.jpg’) or res_url.endswith(’.css’) or res_url.endswith(’.js’) or res_url.endswith(’.png’) or res_url.endswith(’.gif’): passelse: if res_url.find(r’.css?’)!=-1 or res_url.find(r’.js?’)!=-1:pass else:if res_url_dict.get(res_url,’-’)==’-’: res_url_dict[res_url]=1else: res_url_dict[res_url]=res_url_dict[res_url]+1 from_url_start =’ 'http://’ if line.find(from_url_start)!=-1:from_url = ’http://’+line.split(from_url_start)[1].split(’' ’)[0]if from_url_dict.get(from_url,’-’)==’-’: from_url_dict[from_url]=1else: from_url_dict[from_url]=from_url_dict[from_url]+1 logfile.close() yesterday = datetime.date.today()-datetime.timedelta(days=1) yesterday_str = str(yesterday).replace(’-’,’’) conn = pymysql.connect(host=’127.0.0.1’, user=’fenxi’,passwd=’’, db=’rizhifenxi’, port = 3306, charset = ’utf8’) cur = conn.cursor() cur.execute(’insert into categort(yesterday_str, item, question, topic, people, celebrity, brand, page, category, article) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)’, (yesterday_str, category[’item’], category[’question’], category[’topic’], category[’people’], category[’celebrity’], category[’brand’], category[’page’], category[’category’], category[’article’]))#temp = 'insert into ipList(ipList) values ('+ ip_list[j] + ')'#print temp#print (’success connect’) conn.commit() cur.close() conn.close()

代碼如上

報(bào)錯(cuò)如下:

root@iZbp1iqn00z9x3jov6bas1Z:/data/wwwlogs/www# python parselog2.py/data/wwwlogs/www/m.xxx.com-access_20170419.logstart.....Traceback (most recent call last): File 'parselog2.py', line 145, in <module> parse2csv(filename) File 'parselog2.py', line 92, in parse2csv cur.execute(’insert into categort(yesterday_str, item, question, topic, people, celebrity, brand, page, category, article) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)’, (yesterday_str, category[’item’], category[’question’], category[’topic’], category[’people’], category[’celebrity’], category[’brand’], category[’page’], category[’category’], category[’article’]))TypeError: list indices must be integers or slices, not strroot@iZbp1iqn00z9x3jov6bas1Z:/data/wwwlogs/www#

這是哪里錯(cuò)了呢?我想把category的統(tǒng)計(jì)結(jié)果這個(gè)結(jié)果導(dǎo)入到數(shù)據(jù)庫(kù),要怎么修改呢?

category修改成category_dict之后其他取值正常,就是這個(gè)取值失敗,是什么原因呢?

cur.execute(’insert into categort(yesterday_str, item, question, topic, people, celebrity, brand, page, category, article) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)’, (yesterday_str, category_dict[’item’], category_dict[’question’], category_dict[’topic’], category_dict[’people’], category_dict[’celebrity’], category_dict[’brand’], 0, category_dict[’category’], category_dict[’article’]))KeyError: ’category’

問(wèn)題解答

回答1:

看錯(cuò)誤提示,猜想你這里的category類(lèi)型是一個(gè)list,而不是一個(gè)dict,不能category[’item’]這樣來(lái)取值

回答2:

懷疑你把category_dict寫(xiě)成category了

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久精品网| 亚洲第一成人影院 | 亚洲免费av一区 | 久草a√ | 伊人爽 | 天天拍夜夜爽 | 欧美成年视频 | 亚洲成人一区二区在线 | 一区二区三区国产视频 | 一a一片一级一片啪啪 | 成人一区二区三区在线观看 | 天天操夜夜操免费视频 | 亚洲精品美女 | 欧美激情视频一区二区三区在线播放 | 国产粉嫩尤物极品99综合精品 | 国产一区二区三区四区三区四 | 一区二区三区小视频 | 久久久久久亚洲欧洲 | 成人影院av | 99久久精品视频免费 | 99精品久久久 | 夜夜艹| 日本高清视频在线播放 | 国产精品色婷婷久久58 | 亚洲一区视频在线 | 波多野结衣一二三区 | 毛色毛片免费看 | 免费久久久 | 日韩一区二区三区视频 | 国产精品久久久久久久久久妇女 | 天天干天天草 | 日韩久久久久 | 久久伦理电影 | 九九综合九九 | 国产精品日韩欧美一区二区 | 91porn成人精品 | 天天摸天天干 | 国产一区二区在线免费播放 | 免费在线成人网 | 亚洲高清视频在线观看 | 国产探花在线观看视频 |