【问题标题】:how to take certain information in my csv?如何在我的 csv 中获取某些信息?
【发布时间】:2020-01-30 12:39:47
【问题描述】:

这是我的第一行 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,但我总是需要最后一个日期才能将其放入新列。我应该使用什么方法?

【问题讨论】:

    标签: python pandas numpy csv


    【解决方案1】:

    你可以试试:

    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 | |

    【讨论】:

    • 如果我想对整个 csv 执行此操作,我只需使用 pandas 读取它,然后对每一行执行循环并将其写入新列
    • 如果您将csv文件作为文本文件打开,逐行读取数据,处理数据并存储在列表中,则此方法有效。我不知道你如何用 pandas 来管理它。
    【解决方案2】:
    import pandas as pd
    df = pd.read_csv('/Users/gfidarov/Desktop/crosscheck/crosscheck/sheet1')
    r = df.split('|')
    r = r[-4:]
    r = '|'.join(r)
    print(r)
    

    仍然出现错误 AttributeError: 'DataFrame' 对象没有属性 'split'

    【讨论】:

      猜你喜欢
      • 2015-02-10
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-07-08
      • 1970-01-01
      • 2020-07-17
      • 2021-04-05
      相关资源
      最近更新 更多