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

Pandas处理Excel单元格多行内容:逐字符输出问题及提取需求

Fixing Character-by-Character Print Issue with Multiline Excel Cells

Got it, let's break down what's happening and fix your problem:

The Root Cause

When you read an Excel cell with multiline text (like your List of Items: followed by fruits) using pd.read_excel(), pandas stores that entire block as a single string with \n (newline) characters separating each line. Your original code either accidentally iterated over each character in that string (hence the -L -i -s... output) or printed the whole string at once, leaving only the first line with the - prefix.

The Solution

We need to split each cell's multiline string into individual lines, then print each line with the - prefix. Here's the adjusted code:

import pandas as pd
import glob

def ExceltoTxt(file):
    df = pd.read_excel(file, header=None)
    # Loop through each cell in the first column
    for cell_content in df[0]:
        # Split the multiline string into separate lines
        individual_lines = cell_content.split('\n')
        # Print each line with the "-" prefix
        for line in individual_lines:
            # Optional: Skip empty lines if your cell has any
            if line.strip():
                print(f"-{line}")

# Process all Excel files in the directory
for xlsx_files in glob.glob("*.xlsx"):
    print(f"Attempting to convert file {xlsx_files}\n\n")
    ExceltoTxt(xlsx_files)

Why This Works

  • cell_content.split('\n') takes the single multiline string from each cell and turns it into a list of individual lines (e.g., ["List of Items:", "Banana", "Apple", "Orange"]).
  • We then loop through that list to print each line with the - prefix, giving you the clean, line-by-line output you want.

Optional Cleanup

If your Excel cell has extra spaces or empty lines, you can add strip() to tidy up each line:

print(f"-{line.strip()}")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 03:15:10