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
|
import tarfile
import pandas as pd
with tarfile.open("C:/Users/ericm/Documents/EE/V2_GLFI.tar.gz", "r:*") as tar:
for n in tar.getnames():
if n.endswith('.TXT'):
try:
result = [(0, 4), (181, 182), (504, 505), (515, 530), (637, 647)]
df = pd.read_fwf(tar.extractfile(n),colspecs=result, header=None, names=['Societe', 'Statut', 'Code','Montant','Compte'],encoding="utf_8_sig")
df = df.assign(Somme = lambda x: x.Montant * x.Code.replace({'H' : 1, 'S' : -1}))
sansStatut = pd.isnull(df["Statut"])
nom = n.replace(".TXT",".csv")
filename = 'C:/Users/ericm/Documents/NOTEBOOK/PNS/data/' + nom
df[sansStatut].loc[:,['Societe','Compte','Somme']].to_csv(filename,index=False)
except pd.errors.EmptyDataError:
continue |