'find feedback from text file from a list of training_id
I've a list of training_id_list= ['235', '121'] and I also have a sample txt file where feedback is saved in format:
576---Great training---5.0---235
577---Not bad ---5.0---235
feedbacck_id--feedback--rating--training_id
Now, if I want to print the feedback for each training and if each training_id has more than 5 reviews, it should only print 5. how do I do that. I've tried:
for element in training_id_list:
each_element = element
with open(r'sample.txt') as sample_txt:
read_sample_txt = sample_txt.readlines()
for line in read_sample_txt:
feedback_id, feedback, rating, training_id = line.split('---')
if each_element in line:
print(feedback)
# But this method returns nothin
Solution 1:[1]
training_id_list = ['235','233','234'] #your list which contains ids
max_print = 5
for id in training_id_list :
with open(r'abc.txt') as sample_txt:
read_sample_txt = sample_txt.readlines()
read_sample_txt = read_sample_txt[:-1:]
printed_review_count = 1
for line in read_sample_txt:
[feedback_id, feedback, rating,id_text] = line.split('---')
if id in id_text and printed_review_count <= max_print : #last entry is id
print(feedback)
printed_review_count += 1
Input (abc.txt)
576---Great training---5.0---235
577---Not bad ---5.0---235
577---Not bad1 ---5.0---235
577---Not bad2 ---5.0---235
577---Not bad3 ---5.0---235
577---Not bad4 ---5.0---235
576---Great training---5.0---235
577---Not bad ---5.0---235
feedbacck_id--feedback--rating--training_id
Output
Great training
Not bad
Not bad1
Not bad2
Not bad3
You have to just parse the thing correctly.
Assuming the format is always like this:
576---Great training---5.0---235
577---Not bad ---5.0---235
feedbacck_id--feedback--rating--training_id
Note: If the input file contains the last line with feedbacck_id--feedback--rating--training_id , you can ignore it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
