自定义Question列表类加载CSV:对象赋值实现技术问询
Hey there! Let's walk through exactly how to turn those CSV string rows into a list of Question objects that you can work with directly. Here's a step-by-step breakdown with code examples (I'll use C# since it's a common OOP language for this scenario, but you can adapt the logic to your language of choice):
1. Confirm Your Question Class Structure
First, let's assume your Question class has properties that match the columns in your CSV. For example, if your CSV has columns like Id,Title,Content,Difficulty, your class might look like this:
public class Question { // Properties matching CSV columns public int Id { get; set; } public string Title { get; set; } public string Content { get; set; } public string Difficulty { get; set; } // Constructor to initialize all properties public Question(int id, string title, string content, string difficulty) { Id = id; Title = title; Content = content; Difficulty = difficulty; } }
2. Update Your Custom List Class Constructor
In your custom list class (let's call it QuestionList), you'll modify the constructor to process each CSV line, parse the fields, and create Question instances:
using System.IO; public class QuestionList : List<Question> { public QuestionList(string csvFilePath) { // You mentioned you already have the CSV data in a string array—replace this line if needed string[] csvRows = File.ReadAllLines(csvFilePath); // Skip the header row if your CSV has one (adjust index to 0 if no header) for (int i = 1; i < csvRows.Length; i++) { string currentRow = csvRows[i]; // Split the row into individual fields // ⚠️ Note: Simple Split(',') can break if fields contain commas inside quotes. For production, use a CSV library like CsvHelper! string[] fields = currentRow.Split(','); try { // Convert fields to the correct data types (match your Question class properties) int questionId = int.Parse(fields[0].Trim()); string questionTitle = fields[1].Trim(); string questionContent = fields[2].Trim(); string questionDifficulty = fields[3].Trim(); // Create a new Question object and add it to your list Question newQuestion = new Question(questionId, questionTitle, questionContent, questionDifficulty); this.Add(newQuestion); } catch (FormatException ex) { // Handle invalid data (e.g., non-numeric ID) Console.WriteLine($"Error parsing row {i}: {ex.Message}"); } } } }
3. Key Notes for Robustness
- Avoid Simple Split for Complex CSV: If your CSV has fields with commas, quotes, or line breaks, a basic
Split(',')will fail. Use a dedicated CSV parsing library (like CsvHelper for C#,csvmodule for Python, or OpenCSV for Java) to handle edge cases correctly. - Error Handling: Add try-catch blocks to handle invalid data (like a non-numeric ID) so your entire process doesn't crash if one row is malformed.
- Header Handling: Adjust the loop start index (
i = 1) if your CSV doesn't have a header row—set it toi = 0instead.
4. Using the List
Once your QuestionList is set up, you can use it like this:
// Initialize your list with the CSV path QuestionList myQuestions = new QuestionList(@"C:\path\to\your\questions.csv"); // Access individual Question objects Question firstQuestion = myQuestions[0]; Console.WriteLine($"First Question Title: {firstQuestion.Title}");
内容的提问来源于stack exchange,提问作者Jake Doe

