用python脚本对tsv按日期排序



如何重新排列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)

正如您可能看到的,我使用第一行和最后一行作为我想要检查的日期范围,因此我不必逐行检查。如有任何帮助,不胜感激。

您可以这样排序和重写文件:-

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

最新更新