Reputation: 13
I'm new to Python
I have this txt:
======== Data: 00:05:08.627012 =========
1900-01-01 00:05:08.627012 ; 0 ; 1.16198 ; 10000000.0
1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.0
========= Data: 00:05:12.721536 =========
1900-01-01 00:05:08.627012 ; 0 ; 1.16198 ; 10000000.0
1900-01-01 00:05:12.721536 ; 0 ; 1.16209 ; 1000000.0
1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.0
I need to remove '1900-01-01' but save time and change 0 to Bid and 1 to Ask
I stacked with this peace and cant figure out what to do next
readyColumns = []
for row in lines:
row = row.strip()
rowsAsArray = row.split(';')
for element in rowsAsArray:
element = element.split(';')
print(element)
Upvotes: 1
Views: 76
Reputation: 23815
lines_block = '''======== Data: 00:05:08.627012 =========
1900-01-01 00:05:08.627012 ; 0 ; 1.16198 ; 10000000.0
1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.0
========= Data: 00:05:12.721536 =========
1900-01-01 00:05:08.627012 ; 0 ; 1.16198 ; 10000000.0
1900-01-01 00:05:12.721536 ; 0 ; 1.16209 ; 1000000.0
1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.0'''
lines = lines_block.split('\n')
new_lines = []
for line in lines:
if line.startswith('1900-01-01'):
fields = line.split(' ; ')
fields[0] = fields[0][10:]
fields[1] = 'Bid' if fields[1] == '0' else 'Ask'
new_line = ' ; '.join(fields)
new_lines.append(new_line)
else:
new_lines.append(line)
for line in new_lines:
print(line)
output
======== Data: 00:05:08.627012 =========
00:05:08.627012 ; Bid ; 1.16198 ; 10000000.0
00:05:08.627012 ; Ask ; 1.16232 ; 10000000.0
========= Data: 00:05:12.721536 =========
00:05:08.627012 ; Bid ; 1.16198 ; 10000000.0
00:05:12.721536 ; Bid ; 1.16209 ; 1000000.0
00:05:08.627012 ; Ask ; 1.16232 ; 10000000.0
Upvotes: 0
Reputation: 2692
If 1900-01-01
is always at the beginning and a constant, you could cut off the first 11 chars. To replace the 0
and 1
and use replace (if I undetstood you right), e.g.:
row = "1900-01-01 00:05:08.627012 ; 1 ; 1.16232 ; 10000000.0"
row[11:].replace('; 0 ;', '; Bid ;').replace('; 1 ;', '; Ask ;')
>>> '00:05:08.627012 ; Ask ; 1.16232 ; 10000000.0'
Upvotes: 1