M.Girish Babu
M.Girish Babu

Reputation: 39

How to access the content of text file by indexing

00:CC:3F:27:A0:F0 2018-07-13 08:18:03 -0.384 3.477 8.895 11.493 10.407 -4.000 0.003 -20.816 -15.527 78

After reading a text file using readlines() method I am getting data as like above. I want to assigin this data into a list and access one by one values by indexing like list[0] = 00:CC:3F:27:A0:F0 list[1] = 2018-07-13 08:18:03

Upvotes: 0

Views: 962

Answers (3)

AcK
AcK

Reputation: 2133

is that what you need?

my_list = my_string.split()  #my_list == [item0, item1, ...]
for idx,item in enumerate(my_list):
    print(idx,my_list[idx])  #output: 0 item0 ; 1 item1 ; ...

Upvotes: 0

Pete
Pete

Reputation: 3

You can use REGEXs to match the text patterns you are looking for, check out REGEX101 to alter any patterns you may need.

import re
text = '00:CC:3F:27:A0:F0 2018-07-13 08:18:03 -0.384 3.477 8.895 11.493 10.407 -4.000 0.003 -20.816 -15.527 78'
mac = []

mac.append(re.match(r'([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})', text).group(0))
mac.append(re.search(r'(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})', text).group(0))
print(mac)

Upvotes: 0

Glen Bizeau
Glen Bizeau

Reputation: 73

You would have to split your data by the spaces.

string = '00:CC:3F:27:A0:F0 2018-07-13 08:18:03 -0.384 3.477 8.895 11.493 10.407 
-4.000 0.003 -20.816 -15.527 78'

items = string.split()

for item in items:
    print item

Then you could iterate through those items for each thing. The only issue is that the date and time will be separate. But if those data value are in the same place each time, as it looks like they might be, thats easy enough to deal with.

Upvotes: 1

Related Questions