I'm coding a Python script in order to modify a text file. Here's an extract of the file, full of both texts and tables:
$
$ PROPRIETES: COQUE
$
$ ----------------------------------------------------------------------
$ PEAU 2 PLIS PFY_SH/M18 - 10MM NIDA NOMEX RAINURE - 2 PLIS PFY-SH/M18
$ ----------------------------------------------------------------------
$ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
PCOMP 260002 0.0000 1.E+10 HILL 20.00
111121 .240-3 0.0 YES 111122 10.0-3 0.0 NO
111123 .240-3 0.0 YES
...
$ ----------------------------------------------------------------------
$ PROPRIETE MEMBRANE
$ ----------------------------------------------------------------------
PSHELL 888888 888888 0.0002 888888 888888 0.0
MAT1 888888 145.5+2 .33 0.0 1.0-6 20.
$
I need to modify some of the material values, and I know that the tables cannot exceed ten columns, so I coded the following:
import pandas as pd
fic_mat = open('FIC_MAT.DAT', 'r')
# As a string so the whole dataframe stays in str
ctel = '1E-4'
fic_mat_pd = pd.DataFrame(columns=[1,2,3,4,5,6,7,8,9,10])
index_mat = 0
delimiter = 8
read_until_for_rest = delimiter*len(fic_mat_pd.columns)
# From text file to dataframe
with fic_mat as in_file:
for line in in_file:
fic_mat_pd.loc[index_mat] = [line[i:i+delimiter] for i in range(0,read_until_for_rest,delimiter)]
index_mat = index_mat + 1
# Modification of dataframe
for index, row in fic_mat_pd.iterrows():
#########
## POUR MAT1
#########
## A changer: 7eme colonne de la 1ere ligne
if 'MAT1' in row.loc[1]:
row.loc[7] = ctel
#########
## POUR MAT2
#########
## A changer: 3 premières colonnes de la 2eme ligne
if 'MAT2' in row.loc[1]:
fic_mat_pd.loc[index+1, 2] = ctel
fic_mat_pd.loc[index+1, 3] = ctel
fic_mat_pd.loc[index+1, 4] = ctel
#########
## POUR MAT8
#########
## A changer: 2 premières colonnes de la 2eme ligne
if 'MAT8' in row.loc[1]:
fic_mat_pd.loc[index+1, 2] = ctel
fic_mat_pd.loc[index+1, 3] = ctel
#########
## POUR MAT9
#########
## A changer: 9eme colonne de la 3eme ligne + 5 premières colonnes de la 4eme ligne
if 'MAT9' in row.loc[1]:
fic_mat_pd.loc[index+2, 9] = ctel
fic_mat_pd.loc[index+3, 2] = ctel
fic_mat_pd.loc[index+3, 3] = ctel
fic_mat_pd.loc[index+3, 4] = ctel
fic_mat_pd.loc[index+3, 5] = ctel
Now, I have the dataframe I want with the correct values, I am trying to extract it as a .dat file but without a separator, in order to keep the same shape as the initial file. I tried the following:
# The classical
fic_mat_pd.to_csv('MAT', header=None, index = False, sep=" ", quoting=csv.QUOTE_NONE, escapechar=" ")
Output:
$ ------ -------- -------- -------- -------- -------- -------- -------- --------
$
$ P ROPRIETE S: COQUE
$
$ ------ -------- -------- -------- -------- -------- -------- -------- --------
$ PEAU 2 PLIS PF Y_SH/M18 - 10MM NIDA NOM EX RAINU RE - 2 P LIS PFY- SH/M18
$ ------ -------- -------- -------- -------- -------- -------- -------- --------
$ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
PCOMP 260002 0.0000 1.E+10 HILL 20.00
111121 .240-3 0.0 YES 111122 10.0-3 0.0 NO
111123 .240-3 0.0 YES
I then tried:
with open('MAT', 'w') as fd:
for i in range(len(fic_mat_pd)):
print(fic_mat_pd.iloc[i,:], sep='', file=fd)
Output:
Name: 11, dtype: object
1 $ ------
2 --------
3 --------
4 --------
5 --------
6 --------
7 --------
8 --------
9 --------
10 \n
Name: 12, dtype: object
1 $ PEAU 2
2 PLIS PF
3 Y_SH/M18
4 - 10MM
5 NIDA NOM
6 EX RAINU
7 RE - 2 P
8 LIS PFY-
9 SH/M18
10 \n
Also tried:
fic_mat_file = open('MATtry','w')
with fic_mat_file as out_file:
write = csv.writer(out_file)
for index, row in fic_mat_pd.iterrows():
write.writerows(row)
Output:
$, ,M,a,t,ê,³,©
a,u,x, ,C,o,q,u
e, ,:, ,P,F,Y,-
S,H, ,e,t, ,N,O
M,E,X, ,R,A,I,N
U,R,E, ,i,s,s,u
,d,e, ,l,',h,ê
³,©,t,a,g,e, ,e
t, ,d,e,s, ,e,s
Anyone would know how to extract the dataframe without any separator in between cells?
Thank you in advance!
If all values of that dataframe are strings, you can use this:
with open('output.dat', 'w') as fd:
for row in df.values:
fd.write("".join(row)+"\n")