I have a list (from CSV) with the following information structure:
Item 1
NUMBER Random ID
Item 2
NUMBER Random ID
Item 3
Item 4
Item 5
NUMBER Random ID
And I would like to create a new list (CSV) that looks like this:
Item 1 NUMBER Random ID
Item 2 NUMBER Random ID
Item 5 NUMBER Random ID
So I would like to create a string from Item 1,2,3... and the line under it, if the next line doesn't contain the string NUMBER.
I can read the CSV and use it as a list, however I don't know how the track the lines. My first idea is to create a list of dicts where every dict contains the index number of the line and the content, and then I can loop through the list_with_dicts and check the next line from the original list.
raw_list = []
num = 0
list_with_dicts = []
for x in raw_list:
num2 = num + 1
dict1 = {}
dict1['index'] = num2
dict1['çontent'] = x
list_with_dicts.append(dict1)
for d in list_with_dicts:
number_of_next_line = d['index'] + 1
if "NUMBER" in raw_list[number_of_next_line]:
new_string = "%s %s" % (d[content], raw_list[number_of_next_line])
else:
print("String without number")
However I'm not sure that it's the easiest and best way to do it. Is there a simpler workaround?