Power Query : Import csv zippé
Bonjour,
J'essaie d'importer un fichier csv, mais celui-ci est zippé.
En provenance de https://www.fdj.fr/jeux-de-tirage/euromillions-my-million/historique (le premier avec les résultats les plus récents.
Pour ce faire, j'utilise une fonction trouvée sur GitHub, ici https://github.com/hohlick/Power-Query-Excel-Formats/blob/master/UnZip.pq
que voici :
(ZIPFile) =>
let
Header = BinaryFormat.Record([
MiscHeader = BinaryFormat.Binary(14),
BinarySize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
FileSize = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
FileNameLen= BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian),
ExtrasLen = BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian)
]),
HeaderChoice = BinaryFormat.Choice(
BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
each if _ <> 67324752 // not the IsValid number? then return a dummy formatter
then BinaryFormat.Record([IsValid = false, Filename=null, Content=null])
else BinaryFormat.Choice(
BinaryFormat.Binary(26), // Header payload - 14+4+4+2+2
each BinaryFormat.Record([
IsValid = true,
Filename = BinaryFormat.Text(Header(_)[FileNameLen]),
Extras = BinaryFormat.Text(Header(_)[ExtrasLen]),
Content = BinaryFormat.Transform(
BinaryFormat.Binary(Header(_)[BinarySize]),
(x) => try Binary.Buffer(Binary.Decompress(x, Compression.Deflate)) otherwise null
)
]),
type binary // enable streaming
)
),
ZipFormat = BinaryFormat.List(HeaderChoice, each _[IsValid] = true),
Entries = List.Transform(
List.RemoveLastN( ZipFormat(ZIPFile), 1),
(e) => [FileName = e[Filename], Content = e[Content] ]
)
in
Table.FromRecords(Entries)Mais j'obtiens un binary vide (0 byte).
Voir fichier :
Quelqu'un peut m'aider ?
Merci
Salut OOoiste,
La fonction en question date de 9 ans, tu ne pense pas que le format ZIP à changé depuis
As-tu essayé d'accéder directement au lien mis dans la fonction "UnZip" ?
Et pourquoi ne pas récupérer directement le fichier du site de la FDJ
A+
Hello,
la fonction DecompressZip de rogeriopradoj sur github à l'air de fonctionner :
(ZIPFile) =>
// expects full path to the ZIP file, only extracts the first data file after getting its size from the central directory
// https://en.wikipedia.org/wiki/Zip_(file_format)#Structure
let
//read the entire ZIP file into memory - we'll use it often so this is worth it
Source = Binary.Buffer(Web.Contents(ZIPFile)),
// get the full size of the ZIP file
Size = Binary.Length(Source),
//Find the start of the central directory at the sixth to last byte
Directory = BinaryFormat.Record([
MiscHeader=BinaryFormat.Binary(Size-6),
Start=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian)
]) ,
Start = Directory(Source)[Start],
//find the first entry in the directory and get the compressed file size
FirstDirectoryEntry = BinaryFormat.Record([
MiscHeader=BinaryFormat.Binary(Start+20),
FileSize=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger32, ByteOrder.LittleEndian),
UnCompressedFileSize=BinaryFormat.Binary(4),
FileNameLen=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian),
ExtrasLen=BinaryFormat.ByteOrder(BinaryFormat.UnsignedInteger16, ByteOrder.LittleEndian)
]) ,
//figure out where the raw data starts
Offset = 30+FirstDirectoryEntry(Source)[FileNameLen]+FirstDirectoryEntry(Source)[ExtrasLen],
Compressed = FirstDirectoryEntry(Source)[FileSize]+1,
//get the raw data of the compressed file
Raw = BinaryFormat.Record([
Header=BinaryFormat.Binary(Offset),
Data=BinaryFormat.Binary(Compressed)
])
// unzip it
in
Binary.Decompress(Raw(Source)[Data], Compression.Deflate)Appeler comme ceci par exemple :
let
Source = UnZip(MonAdresseFichierZipEuromillions),
#"CSV importé" = Csv.Document(Source,[Delimiter=";", Columns=76, Encoding=65001, QuoteStyle=QuoteStyle.None]),
#"En-têtes promus" = Table.PromoteHeaders(#"CSV importé", [PromoteAllScalars=true])
in
#"En-têtes promus"Ami calmant, J.P
@ JExceL2fr, mon but est de ne pas avoir à télécharger et décompresser 2 fois par semaine le fichier de la FDJ.
En décompressant dans PQ, j'ai juste à actualiser.
@ Jurassic Pork, impeccable. Je vais utiliser cette fonction.
Merci.