簡體   English   中英

沒有分隔符的數據框 to_csv

[英]Dataframe to_csv WITHOUT separator

我正在編寫 Python 腳本以修改文本文件。 這是文件的摘錄,包含文本和表格:

$                                                                          
$                              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.           
$                                                                          

我需要修改一些材料值,我知道表格不能超過十列,所以我編碼如下:

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        

現在,我有我想要的數據框和正確的值,我試圖將它提取為 .dat 文件但沒有分隔符,以保持與初始文件相同的形狀。 我嘗試了以下方法:

# The classical
fic_mat_pd.to_csv('MAT', header=None,  index = False, sep=" ", quoting=csv.QUOTE_NONE, escapechar=" ")

輸出:

$  ------ -------- -------- -------- -------- -------- -------- -------- --------        

$                                                                                                                                                              

$                                                               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 

然后我嘗試:

with open('MAT', 'w') as fd:
        for i in range(len(fic_mat_pd)):
            print(fic_mat_pd.iloc[i,:], sep='', file=fd)

輸出:

   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
                

還試過:

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)

輸出:

$, ,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

任何人都知道如何在單元格之間沒有任何分隔符的情況下提取數據幀?

先感謝您!

如果該數據幀的所有值都是字符串,則可以使用以下命令:

with open('output.dat', 'w') as fd:
    for row in df.values:
        fd.write("".join(row)+"\n")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM