Python find.line не фильтрует строку даты из текстового файла - PullRequest
0 голосов
/ 05 сентября 2018

У меня есть большой текстовый файл сделок с данными, где я хочу фильтровать данные, когда я читаю их в кадр данных panda.

Я не могу заставить его фильтровать / получать данные, когда строка является датой.

2017-07-28 09:39:04.442 Allocation: BUY 7.0 AZN @ 43.665, 
2017-07-28 09:39:07.724 Allocation: BUY 400.0 BT.A @ 3.022, 
2017-07-28 09:39:08.802 Allocation: BUY 604.0 PFC @ 4.442, 
2017-07-28 09:39:03.000 Allocation: SELL 1083 PFC @ 4.4432, 
2017-07-28 09:39:03.000 Allocation: SELL 2350 PCT @ 10.3807, 
2017-07-28 09:39:06.000 Allocation: SELL 2000 PFC @ 4.4565, 
2017-07-28 09:39:07.000 Allocation: BUY 3000 VOD @ 2.21219, 
2017-07-28 09:39:08.000 Allocation: SELL 2518 CLLN @ 0.5927, 

Мой код ниже: он работает, когда фильтр похож на «BP», но не когда это «2017-07-28».

# this is to load the text file into content
with open(file) as f:
    content = f.readlines()

content = [x.strip() for x in content] 

# this is to filter the lines in the data
events = []
for line in content:
    #if (line.find('Action') >0 and line.find('BP') > 0) : 
    if line.find('2017-07-28') > 0:    
        events.append(line.split(' '))

data = pd.DataFrame(events)

Ответы [ 2 ]

0 голосов
/ 07 сентября 2018

Спасибо @wpercy за его ответ, который я вычистил ниже. Однако, кажется, что запуск занимает очень много времени.

fileLoc ='T:\\Risk\\DataDump\\Trades_Test.txt'

with open(fileLoc) as f:
    content = f.readlines()
content = [x.strip() for x in content]

for line in content:
    events = [line.split(' ') for line in content if ('2017-07-25' and 'CRDA') in line]

Немного по-другому, но по какой-то причине он работает намного быстрее. Для справки, файл содержит более 300 тыс. Строк.

file ='T:\\Risk\\Trades_Test.txt'


# this is to load the text file into content
with open(file) as f:
    content = f.readlines()
content = [x.strip() for x in content] 

# this is to filter the lines in the data
events = []
for line in content:
    if (line.find('Action') >0 and line[0:10] == '2017-07-25') :
        events.append(line.split(' '))
    if (line.find('Allocation') >0 and line[0:10] == '2017-07-25'):
        events.append(line.split(' '))
0 голосов
/ 05 сентября 2018

Поскольку каждая строка является просто строкой, вы можете использовать in следующим образом:

for line in content:
    if '2017-07-28' in line: 
        events.append(line.split(' '))

или использование списка

events = [ line.split(' ') for line in content if '2017-07-28' in line ]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...