我正在尝试解析来自 ACME PACKET SBC 的 CDR 记录。大部分数据是 CSV,我遇到的问题是我遇到了一些奇怪的情况,它并不适用于每个 CDR,它只适用于其中一些。我最初开始尝试使用这样的 CSV 阅读器来执行此操作。
带有编辑信息的 CDR
...stop,"{phone_number} sip:{phone_number}@{ip_address}", ""{first_name},{last_name}"sip:{phone_number}@{ip_address}", "NAS-Identifier"...
该领域还有 300 列其他列,但这是我在哪里不足的例子之一。
代码片段
import csv
import io
import gzip
col_names = ['Record-Type', 'Calling-Station-ID', 'Called-Station-ID', 'NAS-Identifier']
with gzip.open(filename, 'r') as f:
reader.csv.DictReader(io.TextIOWrapper(f, newline='\n'), fielnames=COL_NAMES, skipinitialspace=True)
try:
for row in reader:
print('record-type: ' + row['Record-Type'])
print('calling-station-id: ' + row['Calling-Station-ID'])
print('called-station-id: ' + row['Called-Station-ID'])
print('NAS-Identifier: ' + row['NAS-Identifier])
except csv.Error as e:
print(e)
使用以下代码,我得到以下内容
record-type: Stop
called-station-id: {phone_number} sip:{phone_number}@{ip_address}
calling-station-id: {first_name}
NAS-Identifier: {last_name} sip:{phone_number}@{ip_address}
我也尝试用 pandas 阅读这篇文章,并得到大致相同的内容,因为引号中的逗号。
加上 with " 在 quotechar 内,我认为那里也存在问题。
这也是大多数记录上的一些变量,这工作正常,只有几百个有这个问题,但由于我无法判断记录在出现之前是如何格式化的,所以我无法解析这些不同。
非常感谢所有帮助。
谢谢