You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于CSV职称匹配提取文本文件对应行的技术实现需求

Got it, let's tackle this task properly. You need to pull lines from a text file that include any of the job titles (CEO, CTO, COO) stored in a CSV file. Below is the Python implementation you provided, paired with clear input examples and the expected output you're looking for:

Solution Breakdown

First, we'll load all target job titles from the CSV file, then scan each line of the text file to check if it contains any of those titles. Matching lines get collected and outputted.

Python Code
import csv

def extract_matching_lines(text_file, csv_file):
    # Load job titles from CSV
    job_titles = []
    with open(csv_file, mode='r', encoding='utf-8') as csv_f:
        csv_reader = csv.reader(csv_f)
        # Assume each row in CSV holds one job title
        for row in csv_reader:
            if row:  # Skip empty rows in CSV
                job_titles.append(row[0].strip())
    
    # Extract lines from text file that contain any job title
    matching_lines = []
    with open(text_file, mode='r', encoding='utf-8') as txt_f:
        for line in txt_f:
            stripped_line = line.strip()
            if any(title in stripped_line for title in job_titles):
                matching_lines.append(stripped_line)
    
    return matching_lines

# Run the function
if __name__ == "__main__":
    results = extract_matching_lines("staff.txt", "job_titles.csv")
    for line in results:
        print(line)
Input Examples

CSV File (job_titles.csv)

CEO
CTO
COO

Text File (staff.txt)

Liam Carter - Senior Developer
Olivia Reed - CEO
Noah Taylor - Finance Lead
Emma Martinez - CTO
Lucas Anderson - COO
Sophia Garcia - HR Manager

Expected Output

Olivia Reed - CEO
Emma Martinez - CTO
Lucas Anderson - COO

Quick Notes
  • This code is case-sensitive by default. If you need to match titles regardless of capitalization (e.g., "ceo" or "Ceo"), modify the check to:
    if any(title.lower() in stripped_line.lower() for title in job_titles):
    
  • If your CSV has a header row or multiple columns, adjust the job_titles.append(row[0].strip()) line to target the correct column index (e.g., row[1] if titles are in the second column).

内容的提问来源于stack exchange,提问作者Jay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:23:55