0

I have the below information in a txt file which is of 300lines

5.2.7.1  timer value  ($D125)
5.2.7.2  Power back information  ($05B2)
5.2.7.3  Time Since power off  ($05B4)
5.2.7.4  highspeed mode State  ($0730)

I want the below information to be read and stored two different strings

For Example

str1 = timer value
str2 = D125

Need a PYTHON Script , compatible with PYTHON 2.7.5.

4
  • and what have you tried?? We can't just make it for you; you need to show some effort. You can always post a job at upwork if you need someone to write the whole code for you
    – Agam Banga
    Commented May 18, 2017 at 6:15
  • I created and updated the text file with above information in PYTHON , but after that i am stuck . I am a beginner in PYTHON thats the issue
    – NaveeKool
    Commented May 18, 2017 at 6:19
  • This looks more of a requirement than a question!
    – CocoCrisp
    Commented May 18, 2017 at 6:29
  • Still just a hint, use RegEx, make your pattern and use it accordingly.
    – CocoCrisp
    Commented May 18, 2017 at 6:30

2 Answers 2

0

I am also new to python and have no experience, but I managed to develop the code below which is not the best code possible, but I guess the output is exactly what you want:

import re

f = open('a.txt').readlines()
str1 = []
str2 = []
for i in f:
    res = re.sub("\n", '', i)
    res = re.sub("\d.\d.\d.\d  ", "", res)
    str = re.split("\(\$", res)
    str[1] = re.sub("\)", '', str[1])
    str1.append(str[0])
    str2.append(str[1])
print(str1)
print(str2)

As @Milind mentioned, RegEx could help you find patterns.
I assumed that txt file's name as a.txt.

0
str_1= str[str.find(start_str)+len(start_str):str.rfind(end_str)]

All you have to do is initialise start_str = "XXXXXXX" and end_str = "YYYYY".

Not the answer you're looking for? Browse other questions tagged or ask your own question.