Return 词典文本文件列表中最大字段的行 Python?
Return line of largest field in list of dictionaries text file with Python?
我正在编写 Python 脚本。我需要 return 包含文本文件中最大 'uid' 字段的行。
例如,在下面的文本文件示例中:
{
"uid": 683,
"user_id": "2",
"timestamp": datetime.datetime(2020, 5, 17, 16, 39, 54),
"status": 1,
"punch": 0,
}, {
"uid": 684,
"user_id": "4",
"timestamp": datetime.datetime(2020, 5, 17, 16, 41, 20),
"status": 1,
"punch": 0,
}
Return 文本文件例如:
{
"uid": 684,
"user_id": "4",
"timestamp": datetime.datetime(2020, 5, 17, 16, 41, 20),
"status": 1,
"punch": 0,
}
这是我的解决方案,我没有读取文本文件,而是使用了来自字符串变量 text
.
的文本
最终结果(具有最大 uid 的条目)包含在 max_entry
变量中。我将此结果作为字符串写入文本文件 result.txt
.
import datetime
text = """
{
"uid": 683,
"user_id": "2",
"timestamp": datetime.datetime(2020, 5, 17, 16, 39, 54),
"status": 1,
"punch": 0,
}, {
"uid": 684,
"user_id": "4",
"timestamp": datetime.datetime(2020, 5, 17, 16, 41, 20),
"status": 1,
"punch": 0,
}
"""
data = eval('[' + text + ']')
max_entry = max(data, key = lambda e: e['uid'])
print(max_entry)
with open('result.txt', 'w', encoding = 'utf-8') as f:
f.write(str(max_entry))
输出:
{'uid': 684, 'user_id': '4', 'timestamp': datetime.datetime(2020, 5, 17, 16, 41, 20), 'status': 1, 'punch': 0}
你表明你的“文本文件”是一个字典列表。
所以你可以这样做:
import datetime
text_file = {
"uid": 683,
"user_id": "2",
"timestamp": datetime.datetime(2020, 5, 17, 16, 39, 54),
"status": 1,
"punch": 0,
}, {
"uid": 684,
"user_id": "4",
"timestamp": datetime.datetime(2020, 5, 17, 16, 41, 20),
"status": 1,
"punch": 0,
}
def return_highest_ui_line(text_file):
temp = []
for i,sub_dict in enumerate(text_file):
temp.append([sub_dict['uid'],i])
return text_file[sorted(temp)[-1][1]]
return_highest_ui_line(text_file)
output:
{'uid': 684,
'user_id': '4',
'timestamp': datetime.datetime(2020, 5, 17, 16, 41, 20),
'status': 1,
'punch': 0}
我通过以下方式解决了这个问题:
import datetime
with open('C:/Users/UI UX/Desktop/newss.txt') as infile:
for line in infile:
data = eval('[' + line + ']')
max_entry = max(data, key=lambda e: e['uid'])
print(max_entry)
with open('result.txt', 'w', encoding='utf-8') as f:
f.write(str(max_entry))
我正在编写 Python 脚本。我需要 return 包含文本文件中最大 'uid' 字段的行。 例如,在下面的文本文件示例中:
{
"uid": 683,
"user_id": "2",
"timestamp": datetime.datetime(2020, 5, 17, 16, 39, 54),
"status": 1,
"punch": 0,
}, {
"uid": 684,
"user_id": "4",
"timestamp": datetime.datetime(2020, 5, 17, 16, 41, 20),
"status": 1,
"punch": 0,
}
Return 文本文件例如:
{
"uid": 684,
"user_id": "4",
"timestamp": datetime.datetime(2020, 5, 17, 16, 41, 20),
"status": 1,
"punch": 0,
}
这是我的解决方案,我没有读取文本文件,而是使用了来自字符串变量 text
.
最终结果(具有最大 uid 的条目)包含在 max_entry
变量中。我将此结果作为字符串写入文本文件 result.txt
.
import datetime
text = """
{
"uid": 683,
"user_id": "2",
"timestamp": datetime.datetime(2020, 5, 17, 16, 39, 54),
"status": 1,
"punch": 0,
}, {
"uid": 684,
"user_id": "4",
"timestamp": datetime.datetime(2020, 5, 17, 16, 41, 20),
"status": 1,
"punch": 0,
}
"""
data = eval('[' + text + ']')
max_entry = max(data, key = lambda e: e['uid'])
print(max_entry)
with open('result.txt', 'w', encoding = 'utf-8') as f:
f.write(str(max_entry))
输出:
{'uid': 684, 'user_id': '4', 'timestamp': datetime.datetime(2020, 5, 17, 16, 41, 20), 'status': 1, 'punch': 0}
你表明你的“文本文件”是一个字典列表。 所以你可以这样做:
import datetime
text_file = {
"uid": 683,
"user_id": "2",
"timestamp": datetime.datetime(2020, 5, 17, 16, 39, 54),
"status": 1,
"punch": 0,
}, {
"uid": 684,
"user_id": "4",
"timestamp": datetime.datetime(2020, 5, 17, 16, 41, 20),
"status": 1,
"punch": 0,
}
def return_highest_ui_line(text_file):
temp = []
for i,sub_dict in enumerate(text_file):
temp.append([sub_dict['uid'],i])
return text_file[sorted(temp)[-1][1]]
return_highest_ui_line(text_file)
output:
{'uid': 684,
'user_id': '4',
'timestamp': datetime.datetime(2020, 5, 17, 16, 41, 20),
'status': 1,
'punch': 0}
我通过以下方式解决了这个问题:
import datetime
with open('C:/Users/UI UX/Desktop/newss.txt') as infile:
for line in infile:
data = eval('[' + line + ']')
max_entry = max(data, key=lambda e: e['uid'])
print(max_entry)
with open('result.txt', 'w', encoding='utf-8') as f:
f.write(str(max_entry))