python中line.split()的用法及实际使用示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中line.split()的用法及实际使用示例相关的知识,希望对你有一定的参考价值。
参考技术A Python中split()函数,通常用于将字符串切片并转换为列表。split():语法:
拆分字符串。通过制定分隔符将字符串进行切片,并返回分割后的字符串列表[list]
参数:str:分隔符,默认为空格,但不能为空("")
num: 表示分割次数。如果指定num,则分割成n+1个子字符串,并可将每个字符串赋给新的变量
line.strip().split(’,’)
strip()表示删除掉数据中的换行符,split(‘,’)则是数据中遇到‘,’ 就隔开。
python对日志的处理及图形显示
1.处理日志的python程序 def read(log): with open(log, encoding="UTF-8") as f: yield from f def make_js(log, name): res = {} for line in read(log): line = line.split(" ") if len(line) > 10: key = line[10] value = res.get(key, 0) + 1 res[key] = value + 1 # print(res) with open("{}.js".format(name), "w") as f: f.write("res_data={result}".format(result = res)) make_js("store.log", "store") # 日志样式 tail -n 3 /data/www/logs/nginx_log/access/store.log 1.1.1.1 - - [07/Feb/2020:19:38:20 -0800] - - "GET /jslibs/select2/select2.full.min.js?201812 HTTP/1.1" 200 23673 "https://store.wondershare.com/checkout/myCart/?code_id=cae3046452b6d2eb7a88f31a3172d752&machine_no=eus&_ga=2.164934399.1772424582.1581129371-825859103.1580980114" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" 0.000 1.1.1.1 - - [07/Feb/2020:19:38:20 -0800] - - "GET /images/checkout/btn_checkout_en_us.gif HTTP/1.1" 200 2290 "https://store.wondershare.com/checkout/myCart/?code_id=cae3046452b6d2eb7a88f31a3172d752&machine_no=eus&_ga=2.164934399.1772424582.1581129371-825859103.1580980114" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" 0.000 1.1.1.1 - - [07/Feb/2020:19:38:21 -0800] - - "GET /images/cart/cart.gif HTTP/1.1" 200 86 "https://store.wondershare.com/checkout/myCart/?code_id=cae3046452b6d2eb7a88f31a3172d752&machine_no=eus&_ga=2.164934399.1772424582.1581129371-825859103.1580980114" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36" 0.000 2.最终生成日志的store.js res_data={‘302‘: 10, ‘200‘: 90} 3.日志展示的html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ECharts</title> <!-- 引入 echarts.js --> <script src="js/echarts.min.js"></script> <script src="store.js"></script> </head> <body> <!-- 为ECharts准备一个具备大小(宽高)的Dom --> <div id="main" style="width: 600px;height:400px;"></div> <script type="text/javascript"> // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById(‘main‘)); // 指定图表的配置项和数据 var series_data = [], legend_data = []; for (var key in res_data) { series_data.push(res_data[key]), legend_data.push(key); } var option = { title: { text: ‘ECharts 入门示例‘ }, tooltip: {}, legend: { data:[‘status‘] }, xAxis: { data: legend_data }, yAxis: {}, series: [{ name: ‘status‘, type: ‘bar‘, data: series_data }] }; // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); </script> </body> </html>
以上是关于python中line.split()的用法及实际使用示例的主要内容,如果未能解决你的问题,请参考以下文章