def main():
# I'm assuming you can get this far...
lines = [
'1,some stuff 1',
'2,some stuff 2,more stuff',
'2,some stuff 4,candy,bacon',
'3,some stuff 3,this,is,horrible...'
]
# Something to hold your parsed data
data = {}
# Iterate over each line of your file
for line in lines:
# Split the data apart on comma per your example data
parts = line.split(',')
# denote the key is the first part of the split data
key = parts[0]
if key not in data:
# Since there could be multiple values per key we need to keep a
# list of mapped values
data[key] = []
# put the "other data" into the list
index_of_sep = line.find(',')
data[key].append(line[index_of_sep+1:])
# You probably want to return here. I'm printing so you can see the result
print(data)
if __name__ == '__main__':
main()
Result
C:\Python35\python.exe C:/Users/Frito/GitSource/sandbox/sample.py
{'3': ['some stuff 3,this,is,horrible...'], '1': ['some stuff 1'], '2': ['some stuff 2,more stuff', 'some stuff 4,candy,bacon']}
Process finished with exit code 0
3
solved Sorting strings into dictionary, where initial char is the key and the value is a list of all lines starting with that char