2016-11-17 69 views
1

我有日志文件像提取客户ID和他们的订单总额

"01-01-2012 01:13:36 sometext date customerid:1768 orders:3 apples" 
"01-09-2013 01:18:34 sometext date customerid:1567678 orders:4 oranges" 
"08-10-2000 08:08:28 sometext date customerid:156 orders:5 grapes" 

如何创建该报告的客户ID,并在Python他们的订单总数Python程序。感谢您的帮助

注意:我能够使用python内置函数(startswith等)提取客户ID和订单,并保存在不同的列表中,我坚持用客户ID和他们的总订单生成报告。

+5

请说明您通过实际显示您编写的代码来实现困难,并明确指出您的代码在哪里存在困难。从你的尝试中,你的错误/问题在哪里?如果您从当前代码获得某种输出,请显示它当前的样子,然后指出它应该*的样子。 – idjaw

+2

@idjaw请记住,如果我将您的评论保存到我的AutoReviewComments银行? –

+0

@叶肯定!继续:) – idjaw

回答

0
import re 
rex = re.compile("sometext date customer:(\d+) orders;(\d+)") 
output = [] 
for data in logs: 
    b = rex.search(data) 
    output.append({"customer_id":b.group(1), "orders": b.group(2)}) 

print output 

日志从日志文件中的数据(打开文件和readlines方法来读取文件中的数据)

+0

这是行不通的,这是因为'sometext date'部分在这里只是一个占位符,并且会在每行中填充适当的值,这当然不应该是相同的。所以你可能会发现一个更符合输入的通用表达式。 –

+0

非常感谢您的建议和code.how关于在日志文件中的日志开始日期如下 “01-01-2012 01:13:36 somedata:customerid:1234 somedata orders:4 date”please help –

0
data = {} 
with open('log.txt', 'r') as f: 
    for line in f: 
     id_user = [int(s) for s in line.split() if s.isdigit()][0] # this basically means to use the first digit in the line as the user id. 
     if not id_user in data: 
      data[id_user] = [] 
     data[id_user].append(line) 

for id_user, lines in data.items(): 
    print(id_user, len(lines)) 

EDITED以下OP评论:

data = {} 
with open('log.txt', 'r') as f: 
    for line in f: 
     customer_id = [s for s in f.split() if s.find('customerid') != -1][0].split(':')[1] 
     if not customer_id in data: 
      data[customer_id] = [] 
     data[customer_id].append(line) 

for customer_id, lines in data.items(): 
    print(customer_id, len(lines)) 
+0

非常感谢您的建议和code.how有关在日志文件中的日志开始日期如下 “01-01-2012 01:13:36 somedata:customerid:1234 somedata orders:4 date”please help –

+0

我编辑了我的脚本以满足您的需求 –

相关问题