含跨多行Report字段的文本文件转换为Pandas DataFrame的正确方法
Fixing Multi-Line Report Field Parsing in Pandas
Got it, let's sort this out! The problem with pd.read_csv here is that it assumes every newline means a new row, but your Report field spans multiple lines and only concludes with [Report Ends]. That's why the parsing gets messy right after the first line of the Report.
Here's a reliable way to load your file into a proper DataFrame:
Step-by-Step Solution
First, we'll read the entire file manually, split records using the [Report Ends] marker, then extract each field correctly:
import pandas as pd # Read the entire file content at once with open('/Path/to/your/file.txt', 'r') as file: full_content = file.read() # Split off the header line first header, raw_records = full_content.split('\n', 1) # Split records using the report end marker, filter out empty entries individual_records = [rec.strip() for rec in raw_records.split('[Report Ends]') if rec.strip()] # Process each record to extract fields parsed_data = [] for record in individual_records: # Split only at the first two | separators (so Report keeps all content including any |) split_parts = record.split('|', 2) if len(split_parts) == 3: num, name, report = split_parts # Clean up extra whitespace/newlines in the report (adjust as needed) cleaned_report = report.strip() parsed_data.append({ 'Number': num.strip(), 'Name': name.strip(), 'Report': cleaned_report }) # Convert to DataFrame df = pd.DataFrame(parsed_data, columns=['Number', 'Name', 'Report'])
Why This Works
- Reading the whole file: Lets us ignore default newline-based row splitting and use our custom
[Report Ends]marker instead. - Split at first two
|: Ensures we don't accidentally split the Report content if it contains|characters. Everything after the second|stays as the full Report. - Cleaning whitespace: The
.strip()calls get rid of any extra newlines or spaces that might be left around fields. If you want to preserve exact line breaks in the Report, just skip the.strip()on the report variable!
内容的提问来源于stack exchange,提问作者Nick
相关产品推荐
相关产品推荐

