久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

python - KeyError: ’page’這個錯誤要怎么搞?

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

問題描述

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()

代碼如上

報錯如下:

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#

這是哪里錯了呢?我想把category的統計結果這個結果導入到數據庫,要怎么修改呢?

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

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’

問題解答

回答1:

看錯誤提示,猜想你這里的category類型是一個list,而不是一個dict,不能category[’item’]這樣來取值

回答2:

懷疑你把category_dict寫成category了

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 欧美日韩专区 | 亚洲国产视频精品 | 一区二区三区视频在线 | 欧美一区二区三区精品 | 精品久久香蕉国产线看观看亚洲 | 国产中文在线 | 日韩视频中文字幕 | www久| 激情欧美一区二区三区中文字幕 | a级性视频 | 91免费视频| 婷五月综合 | 成人精品视频 | 国产美女高潮视频 | 日韩午夜电影在线观看 | 亚洲精品一区二区三区 | 蜜桃av一区二区三区 | 精品久久久久久久 | 6080yy午夜一二三区久久 | 欧美精品a∨在线观看不卡 欧美日韩中文字幕在线播放 | 日本不卡免费新一二三区 | 黄色免费网 | 欧美日韩一区二区三区 | 黑人粗黑大躁护士 | 欧美日本韩国一区二区三区 | 日韩综合一区 | 国产精品久久一区二区三区 | 欧美日免费| 艹逼网 | 日本一区二区三区四区 | 久久精品国产亚洲blacked | 日韩欧美一区二区在线观看视频 | 国产精品久久久久久福利一牛影视 | 欧美日韩国产一区二区三区 | 欧美第一视频 | 欧美日韩国产在线 | 午夜成人免费视频 | 亚州精品天堂中文字幕 | 欧美一区二区三区免费视频 | 国产a免费 | 99久久99 |