在有序词典中搜索

时间:2019-04-23 12:51:54

标签: python csv oop dictionary

我制作了一个程序,使用此代码从csv文件读取;

{# templates/form/fields.html.twig #}
{% block _places_geosuggest_widget %}
<style>
.geosuggest {

    position: relative;
    width: 70%;

    text-align: left;
  }
  .geosuggest__input {
    width: 100%;
    border: 2px solid transparent;
    box-shadow: 0 0 1px #3d464d;
    padding: .1em .5em;
    -webkit-transition: border 0.2s, box-shadow 0.2s;
            transition: border 0.2s, box-shadow 0.2s;
  }
  .geosuggest__input:focus {
    border-color: #267dc0;
    box-shadow: 0 0 0 transparent;
  }
  .geosuggest__suggests {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    max-height: 25em;
    padding: 0;
    margin-top: -1px;
    background: #fff;
    border: 2px solid #267dc0;
    border-top-width: 0;
    overflow-x: hidden;
    overflow-y: auto;
    list-style: none;
    z-index: 5;
    -webkit-transition: max-height 0.2s, border 0.2s;
            transition: max-height 0.2s, border 0.2s;
  }
  .geosuggest__suggests--hidden {
    max-height: 0;
    overflow: hidden;
    border-width: 0;
  }

  /**
   * A geosuggest item
   */
  .geosuggest__item {
    font-size: 18px;
    font-size: 1rem;
    padding: .5em .65em;
    cursor: pointer;
  }
  .geosuggest__item:hover,
  .geosuggest__item:focus {
    background: #f5f5f5;
  }
  .geosuggest__item--active {
    background: #267dc0;
    color: #fff;
  }
  .geosuggest__item--active:hover,
  .geosuggest__item--active:focus {
    background: #ccc;
  }
  .geosuggest__item__matched-text {
    font-weight: bold;
  }

  </style>
<div id="google_map"></div>
 {% if help is defined %}
        <span class="help-block" id="geosuggest-help">Mekanın koordinatları</span>
    {% endif %}
<script src="https://maps.googleapis.com/maps/api/js?key=YOURGOOGLEAPIKEY&libraries=places"></script>
<script type="text/javascript" src="{{ asset('build/react.js') }}"></script>
<input type="hidden" id="places_coordinates" name="places[coordinates]" required="required" class="form-control form-control">
 {% endblock %}

此代码从我的csv文件创建这些类型的有序词典;

reader = csv.DictReader(open('FakeNameSet.csv', 'r', encoding='utf-8-sig'))
customer_list = []
for line in reader:
    customer_list.append(line)

现在,我需要按名称访问此列表中的人员,但是我不知道如何以这种OrderedDict([('Number', '19'), ('Gender', 'female'), ('NameSet', 'Dutch'), ('GivenName', 'Özgül'), ('Surname', 'Overgaauw'), ('StreetAddress', 'Adriana Noorlandersingel 200'), ('ZipCode', '3065 HE'), ('City', 'Rotterdam'), ('EmailAddress', 'OzgulOvergaauw@superrito.com'), ('Username', 'Shrothem1971'), ('TelephoneNumber', '06-15253488')]), OrderedDict([('Number', '20'), ('Gender', 'female'), ('NameSet', 'Dutch'), ('GivenName', 'Gülseren'), ('Surname', 'Willigenburg'), ('StreetAddress', 'Dingspelstraat 28'), ('ZipCode', '9461 JE'), ('City', 'Gieten'), ('EmailAddress', 'GulserenWilligenburg@teleworm.us'), ('Username', 'Ressoare'), ('TelephoneNumber', '06-92433659')])] 样式访问名称。我已经尝试了2个小时,但似乎没有任何反应。

因此,基本上,我有一个包含这些有序词典的列表,我想在这些词典中搜索特定的人;因此基本上,一个功能如下:

OrderedDict,然后返回该人的所有信息。

1 个答案:

答案 0 :(得分:0)

因此,记录是有序词典,但您可以将客户保留在列表中。如果仅按名称搜索,则将customer_list更改为字典应该可以解决问题:

customers = {}
for line in reader:
    customers['GivenName'] = line

# assuming GivenName is unique, search customers by given name:
customer = customers["Gülseren"]

如果您需要按其他字段进行搜索,我建议您使用pandas数据框:

import pandas as pd

customers = pd.read_csv('FakeNameSet.csv', encoding='utf-8-sig')
# find customers by a given name:
# (doesn't assume uniqueness, you can get several rows)
customers[customers['GivenName] == "Gülseren"]