Reputation: 13
I am using python and trying to read a file line by line and adding those lines in JSON, but i need to check if the line starts with some word and after that word put the text in json until it finds that the line starts with the specific word again,
I have an array of these specific names:
names_array= ['Filan Fisteku','Fisteku Filan']
so for example the txt file is like:
so the json i want to make out of this txt is :
{
"Filan Fisteku":["Said something about this , blla blla blla",
"then the Filan Fisteku speech goes on on the next line,",
"plus some other text."],
"Fisteku Filan":["This is another text from another guy which",
"i am trying to put in a json"]
}
I need to know if I can do this with recursion or how can i do that?
Upvotes: 1
Views: 214
Reputation: 9647
You can use a flag to identify current speaker. And update the flag if you encounter new speaker at the start of a line. And if there is no speaker at the start of the line then the line goes to the current speaker array. I've created a demo, check if that works for you,
speaker = ''
Filan_Fisteku = []
Fisteku_Filan = []
with open('yourfile.txt', 'r') as f:
for line in f.readlines():
if line.startswith('Filan Fisteku:'):
line = line.lstrip('Filan Fisteku:')
Filan_Fisteku.append(line.strip())
speaker = 'Filan Fisteku'
elif line.startswith('Fisteku Filan:'):
line = line.lstrip('Fisteku Filan:')
Fisteku_Filan.append(line.strip())
speaker = 'Fisteku Filan'
elif speaker == 'Filan Fisteku':
Filan_Fisteku.append(line.strip())
elif speaker == 'Fisteku Filan':
Fisteku_Filan.append(line.strip())
mydict = {'Filan Fisteku': Filan_Fisteku, 'Fisteku Filan': Fisteku_Filan}
Frome the data, mydict
will look like this,
{'Filan Fisteku': ['Said something about this , blla blla blla then',
'the Filan Fisteku speech goes on on the next line, plus some other text.',
'plus some other text.'],
'Fisteku Filan': ['This is another text from another guy which',
'i am trying to put in a json.']}
Upvotes: 0
Reputation: 142176
You can build a dict
using the following:
names = {}
with open('yourfile') as fin:
lines = (line.strip().partition(': ') for line in fin)
for fst, sep, snd in lines:
if sep:
name = fst
names.setdefault(name, []).append(snd or fst)
Which gives:
{'Filan Fisteku': ['Said something about this , blla blla blla then',
'the Filan Fisteku speech goes on on the next line, plus some other text.'],
'Fisteku Filan': ['This is another text from another guy which i am trying to put in a json.']}
Then json.dumps
names
.
Upvotes: 1
Reputation: 195
You can do this easily:
res = {}
with open('file.txt', 'r') as f:
for line in f.readlines():
for name in names_array:
if line.startswith(name):
if name not in res:
res[name] = [line]
else:
res[name].append(line)
Perhaps you will also need to remove extra characters at the beginning of the line (spaces etc) but it may be not required.
Upvotes: 1