Me conecté a Amazon S3 y estoy tratando de recuperar datos del contenido JSON de varios cubos usando el siguiente código.
Pero tengo que leer solo archivos JSON específicos, pero no todos. ¿Cómo lo hago?
Código:
for i in bucket: try: result = client.list_objects(Bucket=i,Prefix = 'PROCESSED_BY/FILE_JSON', Delimiter='/') content_object = s3.Object(i, "PROCESSED_BY/FILE_JSON/?Account.json") file_content = content_object.get()['Body'].read().decode('utf-8') json_content = json.loads(file_content) except KeyError: passEjemplo de estructura de cubo.
test-eob/PROCESSED_BY/FILE_JSON/222-Account.json test-eob/PROCESSED_BY/FILE_JSON/1212121-Account.json test-eob/PROCESSED_BY/FILE_JSON/122-multi.json test-eob/PROCESSED_BY/FILE_JSON/qwqwq-Account.json test-eob/PROCESSED_BY/FILE_JSON/wqwqw-multi.jsonDe la lista anterior, solo quiero leer archivos *-Account.json.
¿Cómo puedo conseguir esto?
Hay varias formas de hacer esto en Python . Por ejemplo, verificando si 'stringA' está en 'stringB' :
list1=['test-eob/PROCESSED_BY/FILE_JSON/222-Account.json', 'test-eob/PROCESSED_BY/FILE_JSON/1212121-Account.json', 'test-eob/PROCESSED_BY/FILE_JSON/122-multi.json', 'test-eob/PROCESSED_BY/FILE_JSON/qwqwq-Account.json', 'test-eob/PROCESSED_BY/FILE_JSON/wqwqw-multi.json',] for i in list1: if 'Account' in i: print (i) else: passPuede utilizar una expresión regular que coincida con su patrón de la lista de objetos.
import re MATCH = "FILE_JSON/.*?Account.json" full_list = [ "test-eob/PROCESSED_BY/FILE_JSON/222-Account.json", "test-eob/PROCESSED_BY/FILE_JSON/1212121-Account.json", "test-eob/PROCESSED_BY/FILE_JSON/122-multi.json", "test-eob/PROCESSED_BY/FILE_JSON/qwqwq-Account.json", "test-eob/PROCESSED_BY/FILE_JSON/wqwqw-multi.json" ] for item in full_list: if re.search(MATCH, item): print(item)