python - 如何在元数据之后读取 CSV?
问题描述
我有一个这样的 CSV 文件:
#Description
#Param1: value
#Param2: value
...
#ParamN: value
Time (s),Header1,Header2
243.41745,3,1
243.417455,3,5
243.41746,7,6
...
我需要使用 Python 阅读它,而不需要使用 Pandas。如何读取 CSV 数据本身忽略初始行直到空行?我正在使用下面的代码成功读取元数据。
def read(file_path: str):
'''Read the data of the Digilent WaveForms Logic Analyzer Acquisition
(moodel Discovery2).
Parameter: File path.
'''
meta = {}
RE_CONFIG = re.compile(r'^#(?P<name>[^:]+)(: *(?P<value>.+)\s*$)*')
with open(file_path, 'r') as fh:
# Read the metadata and description at the beginning of the file.
for line in fh.readlines():
line = line.strip()
if not line:
break
config = RE_CONFIG.match(line)
if config:
if not config.group('value'):
meta.update({'Description': config.group('name')})
else:
meta.update({config.group('name'): config.group('value')})
# Read the data it self.
data = csv.DictReader(fh, delimiter=',')
return data, meta
解决方案
这似乎有效。我必须更改for line in fh.readlines():
为for line in fh:
读取元数据的部分,以便不会读取与数据的行,然后创建DictReader
并使用它来获取data
.
import csv
from pprint import pprint, pp
import re
def read(file_path: str):
'''Read the data of the Digilent WaveForms Logic Analyzer Acquisition
(moodel Discovery2).
Parameter: File path.
'''
meta = {}
RE_CONFIG = re.compile(r'^#(?P<name>[^:]+)(: *(?P<value>.+)\s*$)*')
with open(file_path, 'r') as fh:
# Read the metadata and description at the beginning of the file.
for line in fh: # CHANGED
line = line.strip()
if not line:
break
config = RE_CONFIG.match(line)
if config:
if not config.group('value'):
meta.update({'Description': config.group('name')})
else:
meta.update({config.group('name'): config.group('value')})
# Read the data itself.
reader = csv.DictReader(fh, delimiter=',')
data = list(reader)
return data, meta
res = read('mixed.csv')
pprint(res)
推荐阅读
- mysql - SQL:选择表中特定字段集重复的所有条目
- oracle - 不同账户中的 Amazon RDS Oracle 数据库快照还原
- ruby-on-rails - 能够获得最小值,单个查询中的记录数
- kotlin - Is it safe to use Check and Require in production code? - Kotlin
- npm - 如何在未安装 cdk 时使用上下文参数执行 cdk 命令,即使用 npm run cdk?
- java - 将 Java HashMap 转换为海龟三元组
- python - mpdl3 和 lmfit 安装在终端中的 conda 成功,但在 spyder 中导入时未找到
- javascript - Number() 如果为 null 或为空,则默认为 0
- php - “分组” PHP 类的正确方法
- html - 将 div 放在两个 div 之间的边界之上