Bonjour,

Afin d'éviter de décompresser les fichiers qui se trouvent dans le tar.gz (opération qui dure plusieurs heures), je lis celui-ci directement et boucle sur les fichiers txt à l'intérieur:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
import tarfile
import pandas as pd
 
with tarfile.open("C:/Users/ericm/Documents/EE/V2_GLFI.tar.gz", "r:*") as tar:
    txt_path = list(n for n in tar.getnames() if n.endswith('.TXT'))[-1]
    df = pd.read_fwf(tar.extractfile(txt_path), header=None, encoding="utf_8_sig")
A l'intérieur de ce tar.gz, la lecture de fichier vide me semble t-il entraine une erreur avec pandas:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
EmptyDataError                            Traceback (most recent call last)
<ipython-input-28-9427c718b7ae> in <module>
      4 with tarfile.open("C:/Users/ericm/Documents/EE/V2_GLFI.tar.gz", "r:*") as tar:
      5     txt_path = list(n for n in tar.getnames() if n.endswith('.TXT'))[-1]
----> 6     df = pd.read_fwf(tar.extractfile(txt_path), header=None, encoding="utf_8_sig")
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in read_fwf(filepath_or_buffer, colspecs, widths, infer_nrows, **kwds)
    844     kwds["infer_nrows"] = infer_nrows
    845     kwds["engine"] = "python-fwf"
--> 846     return _read(filepath_or_buffer, kwds)
    847 
    848 
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in _read(filepath_or_buffer, kwds)
    450 
    451     # Create the parser.
--> 452     parser = TextFileReader(fp_or_buf, **kwds)
    453 
    454     if chunksize or iterator:
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, engine, **kwds)
    944             self.options["has_index_names"] = kwds["has_index_names"]
    945 
--> 946         self._make_engine(self.engine)
    947 
    948     def close(self):
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in _make_engine(self, engine)
   1187                     'are "c", "python", or "python-fwf")'
   1188                 )
-> 1189             self._engine = klass(self.f, **self.options)
   1190 
   1191     def _failover_to_python(self):
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, **kwds)
   3780         self.colspecs = kwds.pop("colspecs")
   3781         self.infer_nrows = kwds.pop("infer_nrows")
-> 3782         PythonParser.__init__(self, f, **kwds)
   3783 
   3784     def _make_reader(self, f):
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, **kwds)
   2391         # Set self.data to something that can read lines.
   2392         if hasattr(f, "readline"):
-> 2393             self._make_reader(f)
   2394         else:
   2395             self.data = f
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in _make_reader(self, f)
   3783 
   3784     def _make_reader(self, f):
-> 3785         self.data = FixedWidthReader(
   3786             f,
   3787             self.colspecs,
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in __init__(self, f, colspecs, delimiter, comment, skiprows, infer_nrows)
   3675         self.comment = comment
   3676         if colspecs == "infer":
-> 3677             self.colspecs = self.detect_colspecs(
   3678                 infer_nrows=infer_nrows, skiprows=skiprows
   3679             )
 
~\anaconda3\lib\site-packages\pandas\io\parsers.py in detect_colspecs(self, infer_nrows, skiprows)
   3743         rows = self.get_rows(infer_nrows, skiprows)
   3744         if not rows:
-> 3745             raise EmptyDataError("No rows from which to infer column width")
   3746         max_len = max(map(len, rows))
   3747         mask = np.zeros(max_len + 1, dtype=int)
 
EmptyDataError: No rows from which to infer column width
Comment SVP puis je contourner cette erreur sans enlever les fichiers vides?

Merci pour votre aide.

Eric