首页 > 解决方案 > 如何在我的 csv 中获取某些信息?

问题描述

这是我的第一行 csv。我在一列中有 11000 个

"
► Contact with patient | 04.09.2019 |  |
► receive the job | 04.09.2019 |  |
► contact with patient  | 04.09.2019 |  |
► take all docs and read  | 05.09.2019 |  |
► is there any docs to send | 19.09.2019 |  |
► take the contract | 20.09.2019 |  |
► Actualise the contract | 20.09.2019 |  |
► take the contact | 20.09.2019 |  | "

我正在尝试获取此 csv 的最后一个书面部分(► 联系 | 20.09.2019 | |),它们都是不同的,有些有 10 个部分,有些有 2 个,但我总是需要最后一个日期才能将其放入新列。我应该使用什么方法?

标签: pythonpandasnumpycsv

解决方案


你可以试试:

row = """
Contact with patient | 04.09.2019 |  |
receive the job | 04.09.2019 |  |
contact with patient  | 04.09.2019 |  |
take all docs and read  | 05.09.2019 |  |
is there any docs to send | 19.09.2019 |  |
take the contract | 20.09.2019 |  |
Actualise the contract | 20.09.2019 |  |
take the contact | 20.09.2019 |  | """

r = row.split('|')  # split in a list

r = r[-4:]  # keep the 4 last elements

r = '|'.join(r)  # join them together

或在一行中相同:

r = '|'.join(row.split('|')[-4:])

print(r) # 获取联系人 | 20.09.2019 | |


推荐阅读