是否可以读取使用C++的STD::使用Python CSV阅读器的SEtW商店生成的文件?

2024-07-05 09:48:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用C++ +{ }生成的数据文件,例如

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 

Tags: csvcsvfilefordata数据文件readerprecisionfile
2条回答

csv.reader初始值设定项的^{} argument“可以是支持迭代器协议并在每次调用其next()方法时返回字符串的任何对象”

这意味着您可以通过定义如下所示的生成器函数来读取文件,以预处理文件的行,使它们为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

相关问题 更多 >