1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Détecte le type de fichier et son MIME
Détecte l'encodage du fichier ISO2709 (unimarc).
Affichage des notices en utf8. -> Problème
chardet (4.0.0)
pymarc (5.0.0)
decIso5426 (version jurassic pork - https://www.developpez.net/forums/d2151569/autres-langages/python/general-python/encodage-p-fichier-unimarc-iso2709/)
argument 1 -> fichier unimarc
"""
import chardet
import encodings
import magic
import pathlib
import sys
from decIso5426 import decodeIso5426 as dec5426
from pymarc import MARCReader
def analyse(fichier):
print("type de fichier : " + magic.from_file(fichier))
print("type MIME du fichier : " + magic.from_file(fichier, mime = True))
result = chardet.detect(pathlib.Path(fichier).read_bytes())
charenc = str(result)
charencencoding = result['encoding']
print ("l'encodage probable est " + charenc)
with open(fichier, 'rb') as fh:
if charencencoding == "utf-8":
reader = MARCReader(fh, to_unicode=True, force_utf8=True)
analyseurunimarc(reader, charencencoding)
else:
try:
reader = MARCReader(fh, file_encoding=charencencoding)
# lance l'analyseur
analyseurunimarc(reader, charencencoding)
except:
print("problème avec le lecteur.")
pass
def analyseurunimarc(reader, charencencoding):
numnotice = 0 # le compteur de notice
print("Analyseur Unimarc")
print(charencencoding)
for record in reader:
numnotice += 1
print("----------------------------")
print("notice numéro : " + str(numnotice))
# But 1 : voir les notices entières.
# print(record)
# Résultat 1 : les 3 notices s'affichent mais les caractères accentués posent problèmes.'
# But 2 : obtenir le type de l'élement "record" pour voir si le type (souhaité : bytes) ?
#print(type(record))
# => Résultat 2 : class 'pymarc.record.Record'
#ISO-8859-9
#----------------------------
# notice numéro : 1
# <class 'pymarc.record.Record'>
#----------------------------
# notice numéro : 2
# <class 'pymarc.record.Record'>
#----------------------------
# notice numéro : 3
# <class 'pymarc.record.Record'>
# commande python3 / help(pymarc.record) pour voir l'aide module pymarc.
# Si j'ai bien compris la doc de pymarc utiliser : record.as_marc()
# But 3 : Voir les notices
#print(record.as_marc())
# Résultat 3 : Les 3 notices sont bien affichées avec encodage bytes (petit b'xxxxxxxxxxxxx' devant)
# But 4 :
# Affecte la notice dans la variable notice_bytes_encoded /
notice_bytes_encoded = record.as_marc()
# But 5 : Affiche le type (<class 'bytes'>) -> OK
#print(type(notice_bytes_encoded))
# But 6 : Affiche les 3 notices avec échapement des caractères accentués : ex = \x1e' -> OK
#print(notice_bytes_encoded)
#print(repr(notice_bytes_encoded))
# But 7 : Affichage les notices dans le bon encodage (utf8) -> !!! KO !!!
# Utilise la fonction dec5426
try:
print((dec5426(notice_bytes_encoded)))
except:
print("Erreur But7.")
pass
# LE BUT FINAL : Affichage des notices avec les caractères accentués bien reconnus.
#notice_decode = (dec5426(notice_byte_encoded))
#print("la notice : " + numnotice + "en version decodée est :\033[1;32m" + notice_decode + "\033[0m")
if __name__ == "__main__":
fichier = (sys.argv[1])
print("le fichier à analyser est : " + fichier)
analyse(fichier) |