[Solved] Taking a list of stings and converting them in to a list of tuples [closed]


If time ends with - b then you could use normal str.split("- b") to get parts. And you would have to run it in loop

results = []

for line in all_lines:
    parts = line.split(' - b')
    results.append( parts )

or if you want to modify time or data

results = []

for line in all_lines:
    time, data = line.split(' - b')
    # ... here modify `time` or `data`
    results.append( [time, data] )

If time has constant length then you could use slice line[:30]

results = []

for line in all_lines:
    time = line[:30]
    data = line[30+4:]  # len(" - b") == 4
    results.append( [time,data] )

Minimal working example

all_lines = [
    "Sun Nov 17 04:38:17 +0000 2019 - b'RT  <data>",
    "Sun Nov 18 05:38:17 +0000 2020 - b'RT  <data>",
    "Sun Nov 19 06:38:17 +0000 2021 - b'RT  <data>",
]

results = []

for line in all_lines:
    #time, data = line.split(' - b')
    #results.append( [time, data] )

    #parts = line.split(' - b')
    #results.append( parts )
    
    time = line[:30]
    data = line[30+4:]  # len(" - b") == 4
    results.append( [time,data] )
    
# ---

#print(results)

for item in results:
    print(item)

Result:

['Sun Nov 17 04:38:17 +0000 2019', "'RT  <data>"]
['Sun Nov 18 05:38:17 +0000 2020', "'RT  <data>"]
['Sun Nov 19 06:38:17 +0000 2021', "'RT  <data>"]

1

solved Taking a list of stings and converting them in to a list of tuples [closed]