0

I have a log file (.txt) containing the following data structure:

ID-Number 1

value 1

value 2

ID-Number 2

value 1

value 2

value 3

ID-Number 3

value 1

I'm expecting to have the following every ID-Number could have multiple values. I need to put each value beside it's ID-Number like the following:

ID-Number 1,value 1,value 2

ID-Number 2,value 1,value 2,value 3

ID-Number 3,value 1

4
  • do you have id number or how you differentiate id number with value? Commented Jun 17, 2019 at 22:26
  • the string 'ID-Number' is static in each 'ID-Number <int>' field. the integer number is the one that changed every time. Commented Jun 17, 2019 at 22:29
  • Why the pandas tag? Do you need a pandas dataframe out of this file? Commented Jun 17, 2019 at 22:47
  • My goal is to store the final output into CSV or excel format. hence, I added pandas perhaps i will use one of pandas functions to store my data on df and then organize it as per the above structure Commented Jun 17, 2019 at 22:59

1 Answer 1

1
with open(filename, 'r') as f:
    newline = []
    res = []
    for line in f:
        line = line.strip()
        if 'ID-Number' in line:
            if newline:
                res.append(','.join(newline))
            newline = [line]
            continue
        newline.append(line)
res.append(','.join(newline))
Sign up to request clarification or add additional context in comments.

1 Comment

removing res.append(','.join(newline)) worked for me thank you @galaxyan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.