简体   繁体   English

python csv定界符无法正常工作

[英]python csv delimiter doesn't work properly

I try to write a python code to extract DVDL values from the input. 我尝试编写一个python代码从输入中提取DVDL值。 Here is the truncated input. 这是截断的输入。

      A V E R A G E S   O V E R   50000 S T E P S


 NSTEP =    50000   TIME(PS) =     300.000  TEMP(K) =   300.05  PRESS =   -70.0
 Etot   =    -89575.9555  EKtot   =     23331.1725  EPtot      =   -112907.1281
 BOND   =       759.8213  ANGLE   =      2120.6039  DIHED      =      4231.4019
 1-4 NB =       940.8403  1-4 EEL =     12588.1950  VDWAALS    =     13690.9435
 EELEC  =   -147238.9339  EHBOND  =         0.0000  RESTRAINT  =         0.0000
 DV/DL  =        13.0462 
 EKCMT  =     10212.3016  VIRIAL  =     10891.5181  VOLUME     =    416404.8626
                                                Density    =         0.9411
 Ewald error estimate:   0.6036E-04



      R M S  F L U C T U A T I O N S


 NSTEP =    50000   TIME(PS) =     300.000  TEMP(K) =     1.49  PRESS =   129.9
Etot   =       727.7890  EKtot   =       115.7534  EPtot      =       718.8344
BOND   =        23.1328  ANGLE   =        36.1180  DIHED      =        19.9971
1-4 NB =        12.7636  1-4 EEL =        37.3848  VDWAALS    =       145.7213
EELEC  =       739.4128  EHBOND  =         0.0000  RESTRAINT  =         0.0000
DV/DL  =         3.7510
EKCMT  =        76.6138  VIRIAL  =      1195.5824  VOLUME     =     43181.7604
                                                Density    =         0.0891
 Ewald error estimate:   0.4462E-04

Here is the script. 这是脚本。 Basically we have a lot of DVDL in the input (not in the above truncated input) and we only want the last two. 基本上,我们在输入中有很多DVDL(在上面的截断的输入中没有),我们只想要最后两个。 So we read all of them into a list and only get the last two. 因此,我们将它们全部读入列表,仅获得最后两个。 Finally, we write the last two DVDL in the list into a csv file. 最后,我们将列表中的最后两个DVDL写入一个csv文件。 The desire output is 期望输出是

13.0462, 3.7510 13.0462,3.7510

However, the following script (python 2.7) will bring the output like this. 但是,以下脚本(python 2.7)将带来这样的输出。 Could any guru enlighten? 上师能开导吗? Thanks. 谢谢。

13.0462""3.7510"" 13.0462 “” 3.7510 “”

Here is the script: 这是脚本:

import os
import csv

DVDL=[]
filename="input.out"
file=open(filename,'r')

with open("out.csv",'wb') as outfile: # define output name
    line=file.readlines()
    for a in line:
        if ' DV/DL  =' in a:
            DVDL.append(line[line.index(a)].split('        ')[1])  # Extract DVDL number

    print DVDL[-2:]        # We only need the last two DVDL
    yeeha="".join(str(a) for a in DVDL[-2:])
    print yeeha
    writer = csv.writer(outfile, delimiter=',',lineterminator='\n')#Output the list into a csv file called "outfile"
    writer.writerows(yeeha)

As the commenter who proposed an approach has not had the chance to outline some code for this, here's how I'd suggest doing it (edited to allow optionally signed floating point numbers with optional exponents, as suggested by an answer to Python regular expression that matches floating point numbers ): 由于提出此方法的评论者没有机会为此概述代码,因此,我建议这样做(如对Python正则表达式的回答所建议,已编辑为允许带有可选指数的可选带符号浮点数) 匹配浮点数 ):

import re,sys

pat = re.compile("DV/DL += +([+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)")
values = []
for line in open("input.out","r"):
    m = pat.search(line)
    if m:
        values.append(m.group(1))
outfile = open("out.csv","w")
outfile.write(",".join(values[-2:]))

Having run this script: 运行此脚本后:

$ cat out.csv
13.0462,3.7510

I haven't used the csv module in this case because it isn't really necessary for a simple output file like this. 在这种情况下,我没有使用过csv模块,因为对于像这样的简单输出文件,它实际上不是必需的。 However, adding the following lines to the script will use csv to write the same data into out1.csv : 但是,在脚本中添加以下行将使用csv将相同的数据写入out1.csv

import csv

writer = csv.writer(open("out1.csv","w"))
writer.writerow(values[-2:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM