bây giờ tôi có file tên 1.txt chứa : "Logistics giúp nâng cao tỷ suất lợi nhuận lên bao…
- User
bây giờ tôi có file tên 1.txt chứa : "Logistics giúp nâng cao tỷ suất lợi nhuận lên bao nhiêu lần sản xuất thông thường? 1 of 124
3 lần 2 lần 1 lần 4 lần Dòng tài chính thể hiện điều gì trong chuỗi cung ứng? 2 of 124
Thông tin đơn hàng Chất lượng sản phẩm Hiệu quả kinh doanh Quá trình vận chuyển Điều gì là đặc điểm nổi bật của các công ty cung cấp dịch vụ 3PL? 3 of 124
Đa dạng hình thức và phương tiện vận chuyển Chỉ hoạt động trong một khu vực nhất định Không có mối liên hệ với các công ty khác Chỉ sử dụng một phương tiện vận chuyển" và 2.txt chứa: "Logistics giúp nâng cao tỷ suất lợi nhuận lên bao nhiêu lần sản xuất thông thường? Correct Answer 3 lần Your Answer ⠀ Dòng tài chính thể hiện điều gì trong chuỗi cung ứng? Correct Answer Hiệu quả kinh doanh Your Answer ⠀ Điều gì là đặc điểm nổi bật của các công ty cung cấp dịch vụ 3PL? Correct Answer Đa dạng hình thức và phương tiện vận chuyển Your Answer" ⠀ và tôi muốn biến nó thành thế này: "Logistics giúp nâng cao tỷ suất lợi nhuận lên bao nhiêu lần sản xuất thông thường? 3 lần (correct) 2 lần 1 lần 4 lần
Dòng tài chính thể hiện điều gì trong chuỗi cung ứng? Thông tin đơn hàng Chất lượng sản phẩm Hiệu quả kinh doanh (correct) Quá trình vận chuyển
Điều gì là đặc điểm nổi bật của các công ty cung cấp dịch vụ 3PL? Đa dạng hình thức và phương tiện vận chuyển (correct) Chỉ hoạt động trong một khu vực nhất định Không có mối liên hệ với các công ty khác Chỉ sử dụng một phương tiện vận chuyển" tôi muốn dùng python để tự động việc này với 100 câu nữa
- Assistant
To achieve your goal, we'll write a Python script that processes your two text files (
1.txtand2.txt), matches the correct answers to the corresponding questions, and outputs the combined result in the desired format. Below is the script that accomplishes this:# -*- coding: utf-8 -*- def parse_questions(file_path): questions = [] with open(file_path, 'r', encoding='utf-8') as f: lines = f.readlines() idx = 0 total_lines = len(lines) while idx < total_lines: line = lines[idx].strip() if line: question_text = line idx += 1 # Skip the 'n of m' line while idx < total_lines and not lines[idx].strip(): idx += 1 if idx < total_lines and ('of' in lines[idx]): idx += 1 # Collect options options = [] while idx < total_lines and len(options) < 4: opt_line = lines[idx].strip() if opt_line: options.append(opt_line) idx += 1 questions.append({'question_text': question_text, 'options': options}) else: idx += 1 return questions def parse_answers(file_path): answers = [] with open(file_path, 'r', encoding='utf-8') as f: lines = f.readlines() idx = 0 total_lines = len(lines) while idx < total_lines: line = lines[idx].strip() if line: question_text = line idx += 1 # Expect 'Correct Answer' line while idx < total_lines and lines[idx].strip() != 'Correct Answer': idx += 1 idx += 1 # Skip 'Correct Answer' line # Get correct answer correct_answer = '' while idx < total_lines and not lines[idx].strip(): idx += 1 if idx < total_lines: correct_answer = lines[idx].strip() idx += 1 # Skip until 'Your Answer' or next question while idx < total_lines and lines[idx].strip() != 'Your Answer': idx += 1 idx += 1 # Skip 'Your Answer' line answers.append({'question_text': question_text, 'correct_answer': correct_answer}) else: idx += 1 return answers def combine_questions_answers(questions, answers): combined = [] for q, a in zip(questions, answers): question_text = q['question_text'] options = q['options'] correct_answer = a['correct_answer'] # Mark the correct option marked_options = [] for opt in options: if opt == correct_answer: marked_options.append(f"{opt} (correct)") else: marked_options.append(opt) combined.append({'question_text': question_text, 'options': marked_options}) return combined def write_output(combined_questions, output_file): with open(output_file, 'w', encoding='utf-8') as f: for item in combined_questions: f.write(item['question_text'] + '\n') for opt in item['options']: f.write(opt + '\n') f.write('\n') # Add empty line between questions def main(): questions = parse_questions('1.txt') answers = parse_answers('2.txt') combined_questions = combine_questions_answers(questions, answers) write_output(combined_questions, 'output.txt') if __name__ == '__main__': main()Explanation:
-
Parsing
1.txt:
Theparse_questionsfunction reads the questions and options from1.txt. It skips blank lines and the 'n of m' line that follows each question. It collects exactly four options for each question. -
Parsing
2.txt:
Theparse_answersfunction reads the questions and their correct answers from2.txt. It looks for the 'Correct Answer' label to find the correct answer for each question. -
Combining Questions and Answers:
Thecombine_questions_answersfunction matches each question with its correct answer (assuming the order is the same in both files) and marks the correct option with(correct). -
Writing the Output:
Thewrite_outputfunction writes the combined questions and options tooutput.txt, adding an empty line between questions for readability.
Usage:
- Save the script to a file, for example,
process_quiz.py. - Ensure that your
1.txtand2.txtfiles are in the same directory as the script. - Run the script using the command:
python process_quiz.py - The output will be written to
output.txtin the desired format.
Note:
- This script assumes that the questions in
1.txtand2.txtare in the same order. - It also assumes that each question in
1.txthas exactly four options. - The script uses
utf-8encoding to handle Vietnamese characters. - Ensure there are no discrepancies in the formatting of your input files for accurate results.
-