Upload Convertjson.py
Browse files- Convertjson.py +29 -0
Convertjson.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
# Funci贸n para convertir archivo de WhatsApp a JSONL
|
| 5 |
+
def whatsapp_txt_a_jsonl(archivo_txt, archivo_jsonl):
|
| 6 |
+
# Expresi贸n regular para capturar el timestamp, sender y mensaje
|
| 7 |
+
patron = r'\[(.*?)\] (.*?): (.*)'
|
| 8 |
+
|
| 9 |
+
# Abre el archivo de WhatsApp
|
| 10 |
+
with open(archivo_txt, 'r', encoding='utf-8') as txt_file:
|
| 11 |
+
with open(archivo_jsonl, 'w', encoding='utf-8') as jsonl_file:
|
| 12 |
+
for linea in txt_file:
|
| 13 |
+
# Usar expresi贸n regular para extraer los datos
|
| 14 |
+
match = re.match(patron, linea)
|
| 15 |
+
if match:
|
| 16 |
+
timestamp, sender, message = match.groups()
|
| 17 |
+
# Crear un diccionario con los datos
|
| 18 |
+
json_obj = {
|
| 19 |
+
"timestamp": timestamp,
|
| 20 |
+
"sender": sender,
|
| 21 |
+
"message": message
|
| 22 |
+
}
|
| 23 |
+
# Escribir el objeto en formato JSONL (una l铆nea por cada mensaje)
|
| 24 |
+
jsonl_file.write(json.dumps(json_obj, ensure_ascii=False) + '\n')
|
| 25 |
+
|
| 26 |
+
print(f"Archivo {archivo_jsonl} creado exitosamente.")
|
| 27 |
+
|
| 28 |
+
# Convierte el archivo .txt a .jsonl
|
| 29 |
+
whatsapp_txt_a_jsonl('/Users/Lau/Desktop/_chat.txt', '/Users/Lau/Desktop/Chat/output.jsonl')
|