是否可以使用 Python csv reader 读取由 C++ std::setw 生成的文件?

Is it possible to read files generated with C++ std::setw using Python csv reader?

我有一个使用 C++ 生成的数据文件 std::setw 例如

file << std::scientific << std::setprecision(data_precision);  

for (double data : a_data)                                                                                        
   {                                                                                                                 
       file << std::setw(data_width) << data;                                                                    
   }

file << "\n";

是否可以使用 python csv.reader 或类似的方式读取数据?我尝试了以下方法:

with data as csvfile:
    fieldreader = csv.reader(csvfile) 
    next(fieldreader)                                                                                                                                                                    
    for row in fieldreader:                                                                  
       values.append(float(row[0]))                                                                              

输出整个第一行,表示整行存储为一个条目。我也尝试了一些不同的分隔符,例如\t 没有帮助。

示例输出如下:

#          z        phi               phi1          Massless 
 -16.0000000  0.0000000   9.9901854997e-01  1.0910677716e-19
 -16.0000000  0.0245437   9.9871759471e-01  1.6545142956e-05
 -16.0000000  0.0490874   9.9781493216e-01  3.3051500271e-05
 -16.0000000  0.0736311   9.9631097893e-01  4.9477653557e-05
 -16.0000000  0.0981748   9.9420658732e-01  6.5784269579e-05 

csvfile argumentcsv.reader 初始值设定项 "can be any object which supports the iterator protocol and returns a string each time its next() method is called"。

这意味着您可以通过定义如下所示的生成器函数来读取文件,以预处理文件的行,使它们可以被 csv.reader:

import csv

def preprocess(file):
    for line in file:
        yield ','.join(line.split())

values = []
with open('cppfile.txt') as file:
    fieldreader = csv.reader(preprocess(file))
    next(fieldreader)
    for row in fieldreader:
        print(f'row={row}')
        values.append(float(row[0]))

print()
print(values)

输出:

row=['-16.0000000', '0.0000000', '9.9901854997e-01', '1.0910677716e-19']
row=['-16.0000000', '0.0245437', '9.9871759471e-01', '1.6545142956e-05']
row=['-16.0000000', '0.0490874', '9.9781493216e-01', '3.3051500271e-05']
row=['-16.0000000', '0.0736311', '9.9631097893e-01', '4.9477653557e-05']
row=['-16.0000000', '0.0981748', '9.9420658732e-01', '6.5784269579e-05']

[-16.0, -16.0, -16.0, -16.0, -16.0]

我会选择pandas,一个出色的第三方库,提供高性能、易用的数据结构和数据分析工具,来解析您提到的生成文件:

example.txt

#          z        phi               phi1          Massless 
 -16.0000000  0.0000000   9.9901854997e-01  1.0910677716e-19
 -16.0000000  0.0245437   9.9871759471e-01  1.6545142956e-05
 -16.0000000  0.0490874   9.9781493216e-01  3.3051500271e-05
 -16.0000000  0.0736311   9.9631097893e-01  4.9477653557e-05
 -16.0000000  0.0981748   9.9420658732e-01  6.5784269579e-05 

test.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pandas as pd

if __name__ == "__main__":
    df = pd.read_csv("test.txt", sep=r'\s+', skiprows=1, names=["z", "phi", "phi1", "Massless",])
    print(df)

在运行命令之后如下:

python test.py

我得到以下结果:

      z       phi      phi1      Massless
0 -16.0  0.000000  0.999019  1.091068e-19
1 -16.0  0.024544  0.998718  1.654514e-05
2 -16.0  0.049087  0.997815  3.305150e-05
3 -16.0  0.073631  0.996311  4.947765e-05
4 -16.0  0.098175  0.994207  6.578427e-05