1

我正在导入一个在大多数需要删除的单元格内容末尾带有空格的 excel 文件。以下脚本适用于示例数据:

import pandas as pd

def strip(text):
    try:
        return text.strip()
    except AttributeError:
        return text

def num_strip(text):
    try:
        return text.split(" ",1)[0]
    except AttributeError:
        return text

def parse_excel_sheet(input_file, sheet):
    df = pd.read_excel(
        input_file,
        sheetname= sheet,
        parse_cols = 'A,B,C',            
        names=['ID', 'name_ITA', 'name_ENG'],
        converters = {
            'ID' : num_strip,
            'name1' : strip,
            'name2' : strip,
            }
        )
    return df

file = 'http://www.camminiepercorsi.com/wp-content/uploads/excel_test/excel_test.xlsx'
df = parse_excel_sheet(file,'1')
print(df)

但是,在较大文件上尝试脚本时,解析第一列'ID'不会删除空格。

file = 'http://www.camminiepercorsi.com/wp-content/uploads/excel_test/DRS_IL_startingpoint.xlsx'
df = parse_excel_sheet(file,'test')
print(df)
4

1 回答 1

0

我刚刚运行您的代码,发现空格已从较大文件中的“ID”列中正确删除:

for i, el in enumerate(df['ID'].values):
# print(i)
if " " in el:
    print(el)

从“ID”列中不返回任何元素:这 28 个元素中没有空格。你是如何确认不是这种情况的?

于 2017-09-27T09:54:12.620 回答