您好,我有一個包含多個資訊的大文本檔案。我只想使用 python 程式或工具提取電子郵件 ID 和電話號碼。
HTTP/1.1 200 OK
{"id":"269","first_name":"N S","last_name":"","balance":"0","phonecode":null,"mobile":null,"email":"[email protected]","verified":"0","password":""}
HTTP/1.1 200 OK
{"id":"303","first_name":"Devi","last_name":"Baruah","balance":"0","phonecode":null,"mobile":null,"email":"[email protected]","verified":"0","password":""}
HTTP/1.1 200 OK
{"id":"306","first_name":"Rashmi","last_name":"Kumari","balance":"24","phonecode":"91","mobile":"9xxxxxxx","email":"[email protected]","verified":"1","password":"xxxx"}
HTTP/1.1 200 OK
{"id":"308","first_name":"ashwini","last_name":"gokhale","balance":"7","phonecode":"1","mobile":"61xxxx","email":"[email protected]","verified":"1","password":"xxxxxxx"}
HTTP/1.1 200 OK
{"id":"307","first_name":"Rama","last_name":"De","balance":"0","phonecode":"91","mobile":"73xxxxxx","email":"[email protected]","verified":"1","password":"xxxx"}
uj5u.com熱心網友回復:
如果您的檔案名為test.txt,則可以使用以下代碼段決議檔案中的 json 部分,一次一行:
import json
items = []
with open("test.txt") as file_handle:
for line in file_handle:
try:
if item := json.loads(line):
items.append(item)
except json.decoder.JSONDecodeError:
pass
# 'items' is a list of dictionaries that contain each user's details.
# If you want to extract the IDs, email addresses and phone numbers into separate lists, one way to do it is:
ids = [item.get("id") for item in items]
email = [item.get("email") for item in items]
mobile = [item.get("mobile") for item in items]
uj5u.com熱心網友回復:
看起來這是來自網路服務器的日志。如果可能,請嘗試先創建一個更干凈的檔案
:
import json
mandatory_keys = ['email', 'mobile']
file_str = []
out = []
with open('test') as fd:
file_str = [x.rstrip('\n') for x in fd.readlines() if x.startswith('{')]
for j_str in file_str:
try:
j = json.loads(j_str)
assert [x for x in mandatory_keys if x in j.keys()] == mandatory_keys, f'missing mandatory_keys'
out.append({k: v for k, v in j.items() if k in mandatory_keys})
except:
raise ValueError('Something wrong with the json')
print(out)
此外,您可能希望使用一些 json 模型驗證器作為 'jsonschema' 來替換那里的斷言行并有一個明確的錯誤訊息。
更改mandatory_key 串列,您可以輕松更新您的輸出。
uj5u.com熱心網友回復:
empty_list=[]
with open('test.txt', 'rt') as reading:
for line in reading:
cleaned = line.rstrip('\n') # assume each field to be in new line, strip whitespace
empty_list.append(cleaned)
print(empty_list)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/361629.html
