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

火山引擎 最新活动