【问题标题】:Sort a tsv by date with python script使用 python 脚本按日期对 tsv 进行排序
【发布时间】:2021-11-01 06:03:20
【问题描述】:

如何重新排序 TSV 中的值排列以按日期排序。当前排列是按第一列,但我想用仅按日期排列的相同数据覆盖文件。

目前的格式如下图:

param1 | param2 | param3 | date | param5 | param6 | 

示例内容:

1000045|sample 1|10-Q|2021-02-11|edgar/data/1000045/0001564590-21-005399.txt|edgar/data/1000045/0001564590-21-005399-index.html
1000045|sample 1|4/A|2021-02-12|edgar/data/1000045/0001398344-21-003309.txt|edgar/data/1000045/0001398344-21-003309-index.html
1000045|sample 1|4|2021-02-08|edgar/data/1000045/0001496701-21-000001.txt|edgar/data/1000045/0001496701-21-000001-index.html
1000045|sample 1|4|2021-02-09|edgar/data/1000045/0001398344-21-002769.txt|edgar/data/1000045/0001398344-21-002769-index.html
1000045|sample 1|8-K|2021-01-25|edgar/data/1000045/0001564590-21-002004.txt|edgar/data/1000045/0001564590-21-002004-index.html
1000045|sample 1|8-K|2021-02-03|edgar/data/1000045/0001564590-21-003940.txt|edgar/data/1000045/0001564590-21-003940-index.html
1000045|sample 1|8-K|2021-03-08|edgar/data/1000045/0001564590-21-011365.txt|edgar/data/1000045/0001564590-21-011365-index.html
1000045|sample 1|SC 13G/A|2021-02-08|edgar/data/1000045/0001104659-21-013485.txt|edgar/data/1000045/0001104659-21-013485-index.html
1000045|sample 1|SC 13G/A|2021-02-11|edgar/data/1000045/0001037389-21-000122.txt|edgar/data/1000045/0001037389-21-000122-index.html
1000045|sample 1|SC 13G/A|2021-02-12|edgar/data/1000045/0000354204-21-000071.txt|edgar/data/1000045/0000354204-21-000071-index.html
1000097|sample 2|13F-HR|2021-02-16|edgar/data/1000097/0001000097-21-000004.txt|edgar/data/1000097/0001000097-21-000004-index.html
1000097|sample 2|SC 13G|2021-01-11|edgar/data/1000097/0000919574-21-000165.txt|edgar/data/1000097/0000919574-21-000165-index.html
1000177|sample 3|SC 13G/A|2021-01-29|edgar/data/1000177/0000834237-21-004594.txt|edgar/data/1000177/0000834237-21-004594-index.html

我尝试使用 bash,但它需要与我将使用的脚本同时运行。

我目前的脚本是这样的:

def edgar_filings_download(date): 
    try:
        # code to rearrange the file
        with open(edgar_path + filename, 'r') as file:
            tsv_file = list(csv.reader(file, delimiter='|'))
            _CHECK = datetime.datetime.strptime(date, "%Y-%m-%d")
            start_date = datetime.datetime.strptime(tsv_file[len(tsv_file) - 1][3], "%Y-%m-%d")
            end_date = datetime.datetime.strptime(tsv_file[0][3], "%Y-%m-%d")
            if start_date <= _CHECK <= end_date:
                logger.debug('pass')
            else:
                logger.debug('no pass')
    except Exception as e:
        logger.error(e)

如您所见,我使用第一行和最后一行作为我要检查的日期范围,因此我不必逐行检查。任何帮助表示赞赏。

【问题讨论】:

    标签: python django csv


    【解决方案1】:

    您可以像这样对文件进行排序和重写:-

    import csv
    
    with open('edgar.tsv', 'r+') as tsv:
        tsv_file = sorted(list(csv.reader(tsv, delimiter='|')), key=lambda t: t[3])
        tsv.seek(0)
        for line in tsv_file:
            tsv.write('|'.join(line) + '\n')
    

    【讨论】:

    • 这很棒!谢谢。虽然我想知道,在执行后一个脚本之前重写文件对我来说会更好吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 2015-12-04
    相关资源
    最近更新 更多