根据CSV文件中的数据在字典中创建字典

时间:2019-06-13 06:28:39

标签: python python-3.x dictionary

我想根据CSV数据在字典中创建字典。事情是我还不知道要这样做,我已经尝试在这里和其他站点上搜索答案,但是对于我来说,答案总是空白或太复杂。

我用于读取CSV文件的示例代码。

columnnames =  ["address", "date", "time", "resource", "method", "endpoint", "pre_params", "cond_ep", "even_ep", "version", "status", "tail"]
data = pd.read_csv("log.csv", header=None, skiprows=2, names=columnnames)

使用python从read_csv中采样结果:

['127.0.0.1', '12-Jun-2019', '00:0:00', '0', 'GET', '/api/test_url.php?client_code=MAM01', '/api/test_url.php', '/api/', 'api', 'HTTP/1.1', '200', '80 -"']
['127.0.0.1', '12-Jun-2019', '00:0:04', '0', 'GET', '/api/test_url.php?client_code=test', '/api/test_url.php', '/api/', 'api', 'HTTP/1.1', '200', '80 -"']
['127.0.0.1', '12-Jun-2019', '00:0:00', '0', 'GET', '/api/test_url.php?client_code=test', '/api/test_url.php', '/api/', 'api', 'HTTP/1.1', '200', '80 -"']

所以基本上我想创建一个示例字典:

sample_dictionary = {
                 "/api/test_url.php?client_code=MAM01":{
                               "address": "127.0.0.1",
                               "time": "00:0:04",
                               "date": "12-Jun-2019",
                               "status": "GET",
                               "count": 1
                       },
                  "/api/test_url.php?client_code=test":{
                               "address": "127.0.0.1",
                               "time": "00:00",
                               "date": "12-Jun-2019",
                               "status": "GET",
                               "count": 2
                       }
                 }

如果 api 属于相同的日期时间,则计数键将加1。

4 个答案:

答案 0 :(得分:0)

尝试这样:

<div class="overlay"></div>
<div class="box">

  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <!--in order to make the svg span the entirety of its parent, I set width and height attributes
  for it to 100%-->
  <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  <svg width="100%" height="100%" preserveAspectRatio="none" viewbox="0 0 200 100">
    <polygon points="0,100 100,100 0,50 "
          style="stroke:#000000;"/>
    <polygon points="0,50 100,100 50,0 0,0 "
          style="stroke:#000000;"/>
    <polygon points="100,100 50,00 150,0"
          style="stroke:#000000;"/>
    <polygon points="100,100 200,50 200,0 150,0"
          style="stroke:#000000;"/>
    <polygon points="100,100 200,100, 200,50"
          style="stroke:#000000;"/>
  </svg>
</div>

答案 1 :(得分:0)

  1. 按日期对csv排序
  2. 解析行

(您可以按任意顺序执行1,2)

  1. Python
ret = {}
for i,row in enumerate(rows):
    #check for duplicate times
    count = 0
    index = i
    while(row[1] == rows[i+1][1] and row[2] == rows[i+1][2]): 
      count += 1
      index +=1
    ret[str(row[5])] = {
        'address': row[0],
        'time': row[2],
        'date': row[3],
        'status': row[4],
        'count': count
    }


答案 2 :(得分:0)

Python不允许使用dictionary创建duplicate key作为API URL作为字典键的counter,您应该使用dictionary key变量作为{ {1}}。就像我使用list index作为字典键一样。

from dateutil import parser
from datetime import datetime

rows = [
['127.0.0.1', '12-Jun-2019', '00:0:00', '0', 'GET', '/api/test_url.php?client_code=MAM01', '/api/test_url.php', '/api/', 'api', 'HTTP/1.1', '200', '80 -"'],
['127.0.0.1', '12-Jun-2019', '00:0:04', '0', 'GET', '/api/test_url.php?client_code=test', '/api/test_url.php', '/api/', 'api', 'HTTP/1.1', '200', '80 -"'],
['127.0.0.1', '12-Jun-2019', '00:0:00', '0', 'GET', '/api/test_url.php?client_code=test', '/api/test_url.php', '/api/', 'api', 'HTTP/1.1', '200', '80 -"']
]

sample_dictionary = {}
for index, row in enumerate(rows):
    count=0
    date = parser.parse(row[1])
    if date.date() == datetime.now().date():
        count = 1

    sample_dictionary[index] = dict(api=row[5],address=row[0], time=row[2], date=row[1], status=row[4],
                                             count=count)

print(sample_dictionary)

答案 3 :(得分:0)

csv文件中没有计数

您可以使用以下代码,也可以添加计数


res = {}
with open("sample.csv","r")as f:
  data = csv.reader(f)
  for row in data:
        res[row[5]] = {
        "address":row[0],
        "time":row[2],
        "date":row[1],
        "status":[4]
        }
print(res)```


**Note.***  : What you are using as a key is not uniqe so every time you are replacing with new data
Please **make sure in dictionary you key should be uniqe**