【问题标题】:sort contact list and store output in JSON using python使用 python 对联系人列表进行排序并以 JSON 格式存储输出
【发布时间】:2019-01-08 04:43:13
【问题描述】:

我有一个联系人列表文件“input.txt”,我想按姓氏、名字排序。该文件还有一个喜欢的颜色和邮政编码。

该文件的内容结构如下:

LastName, FirstName, (123)-123-1234, Blue, 11013
FirstName LastName, Green, 12345, 123 123 1234
FirstName, LastName, 12345, 123 123 1234, Yellow

输入.txt:

Annalee Loftis, green, 74339, 360 105 7811
Quinton, Liptak, 87225, 194 974 1020, yellow
Demming, Kent, (753)-851-4445, blue, 81403
Humperdink, Englebert G., (232)-955-1267, aqua marine, 28278
Corliss Nurse, green, 123123121, 640 163 2354

这是我迄今为止在 python 3 中编写的代码。

import os
import string
import re
f = open("input.txt", "r")
for i in f:
  if i and i[0].isalpha(): // proccess lines that starts with alphabet
    a = str((i.split(", ")))
    print(a)

现在,如何在 JSON 中按姓、名和输出排序?

谢谢。

【问题讨论】:

  • 您的代码甚至无法确定名称的不同部分在哪里

标签: python python-3.x sorting data-structures


【解决方案1】:

您可以为内置函数sorted() 编写自定义key 函数。解析通过re模块完成。

import re
from pprint import pprint

# the last and first name table:
# LastName, FirstName, (123)-123-1234, Blue, 11013
# FirstName LastName, Green, 12345, 123 123 1234
# FirstName, LastName, 12345, 123 123 1234, Yellow

data = """
Annalee Loftis, green, 74339, 360 105 7811
Quinton, Liptak, 87225, 194 974 1020, yellow
Demming, Kent, (753)-851-4445, blue, 81403
Humperdink, Englebert G., (232)-955-1267, aqua marine, 28278
Corliss Nurse, green, 123123121, 640 163 2354"""

g = re.findall(r'^(.*?), (.*?), (.*?), (.*?)$', data, flags=re.M)

def get_first_last(v):    
    if v[1].lower() == 'green':
        first_name, last_name = v[0].split()
    elif v[-1].lower() == 'yellow':
        first_name, last_name = v[0], v[1]
    else:
        first_name, last_name = v[1], v[0]

    return last_name, first_name

pprint(sorted(g, key=lambda v: get_first_last(v)))

这将打印:

[('Demming', 'Kent', '(753)-851-4445', 'blue, 81403'),
 ('Humperdink', 'Englebert G.', '(232)-955-1267', 'aqua marine, 28278'),
 ('Annalee Loftis', 'green', '74339', '360 105 7811'),
 ('Corliss Nurse', 'green', '123123121', '640 163 2354'),
 ('Quinton', 'Liptak', '87225', '194 974 1020, yellow')]

要将列表输出为 Json,您可以这样做:

sorted_data = (sorted(g, key=lambda v: get_first_last(v)))
pprint(json.dumps(sorted_data, indent=4))

哪些打印:

('[\n'
 '    [\n'
 '        "Demming",\n'
 '        "Kent",\n'
 '        "(753)-851-4445",\n'
 '        "blue, 81403"\n'
 '    ],\n'
 '    [\n'
 '        "Humperdink",\n'
 '        "Englebert G.",\n'
 '        "(232)-955-1267",\n'
 '        "aqua marine, 28278"\n'
 '    ],\n'
 '    [\n'
 '        "Annalee Loftis",\n'
 '        "green",\n'
 '        "74339",\n'
 '        "360 105 7811"\n'
 '    ],\n'
 '    [\n'
 '        "Corliss Nurse",\n'
 '        "green",\n'
 '        "123123121",\n'
 '        "640 163 2354"\n'
 '    ],\n'
 '    [\n'
 '        "Quinton",\n'
 '        "Liptak",\n'
 '        "87225",\n'
 '        "194 974 1020, yellow"\n'
 '    ]\n'
 ']')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    相关资源
    最近更新 更多