如何用Python的Pandas库提取Excel中独立纯文本格式的指定行
Got it, let's sort this out for you! The problem with your current code is that dataframe.ix[1] returns a Pandas Series object, which includes column labels when printed—hence why you're seeing column names paired with your row values. Since you want to treat the Excel file as just a collection of rows and output clean, label-free plain text, here's how to adjust things:
First, a quick heads-up: .ix is deprecated in newer Pandas versions (1.0+), so we'll use .iloc instead for positional row indexing—it's more reliable and supported long-term.
Option 1: Print each value in the row on a separate line
If you want each entry from the target row to be its own standalone line of text:
import pandas as pd path = r"C:\Users\Utilizador\Desktop\vendas2017.xlsx" dataframe = pd.read_excel(path) # Grab the second row (index 1) as raw values (no column labels attached) row_values = dataframe.iloc[1].values # Print each value one per line for value in row_values: print(value)
This will output:
323 1 0 0
Option 2: Print the entire row as a single delimited line
If you prefer the row values joined into one line (matching the | separator from your example):
import pandas as pd path = r"C:\Users\Utilizador\Desktop\vendas2017.xlsx" dataframe = pd.read_excel(path) # Convert row values to strings and join with your chosen separator row_text = " | ".join(map(str, dataframe.iloc[1].values)) print(row_text)
This will output:
323 | 1 | 0 | 0
Bonus: Iterate over all rows (treat the file as a full row collection)
If you want to process every row in the file instead of just one, loop through all rows like this:
import pandas as pd path = r"C:\Users\Utilizador\Desktop\vendas2017.xlsx" dataframe = pd.read_excel(path) # Loop through each row in the dataframe for _, row in dataframe.iterrows(): # Use either option from above here # Example: Print each value per line, then a blank line between rows for readability for value in row.values: print(value) print()
The core fix here is using .iloc[] to target the row by position, then accessing .values to pull out just the raw data without column labels. This lets you treat the file as a simple list of rows rather than a labeled table structure.
内容的提问来源于stack exchange,提问作者ahax

