"行终止符"关键字参数在 csv.read 中不起作用



我在一个文件中有以下数据:

'#export_datex01video_idx01namex01long_descriptionx01episode_production_numberx02n'

这看起来很简单,可以解析:

reader = csv.DictReader(f, delimiter=u'x01', lineterminator=u'x02n')

但是,当我执行以下操作时,它不能正确解析换行符之前的项。下面是我得到的:

{
    '#export_date': u '1475711237318',
    'video_id': u '382992872'
    'name': u 'Death Lives',
    'long_description': u 'When Peter skips out on his anniversary date with Lois in order to play golf with his buddies, he is inexplicably struck by lightning and visited by Death. Instead of escorting Peter to the after-life, Death tells Peter that Lois will leave him in the future unless Peter uses his near-death experience to come to a life-changing revelation. In order to help him, Death takes Peter back to the moment that Peter and Lois met and fell in love.',
    'episode_production_numberx02': u '2ACX21x02',
}

我该如何解决这个问题,为什么这里没有行终止符?

根据https://docs.python.org/3/library/csv.html#csv.Dialect.lineterminator,读者忽略了lineterminator关键字参数:

Dialect.lineterminator

用于终止写入器生成的行的字符串。默认为'rn'。

注意:阅读器被硬编码为识别'r'或'n'作为行尾,并忽略行尾符。此行为将来可能会改变。

最新更新