将包含制表符的 Excel 文件读入 Pandas Dataframe

问题描述 投票:0回答:0

我的 excel 文件在描述列中有制表符和换行符。

我正在将文件加载到 Pandas 数据框中。

我的目标是用单个分号和空格替换所有特殊字符序列,然后将数据再次写为 CSV 文件。

生成的CSV文件有tab的地方都是乱码。我不确定问题从哪里开始,但我怀疑是在 Excel 文件的加载中,而不是在 CSV 文件的写入中,因为结果显示换行符和制表符已被替换。

我希望

read_excel
函数具有可以解决此问题的参数。

这是加载文件的代码:

import sys
import glob
import pandas as pd

# Extract data from Excel files and merge them into a single Pandas Dataframe.
def extract(input_files, sheet_name):
    # Excel files in the path
    file_list = glob.glob(input_files + "/*.xls*")

    print(f'Number of files to load: {len(file_list)}')
    
    # list of data frames read from excel files we want to merge.
    excl_list = []
 
    for file in file_list:
        excl_list.append(pd.read_excel(io=file, sheet_name=sheet_name, header=0))
 
    # create a new dataframe to store the
    # merged data file.
    excl_merged = pd.concat(excl_list, axis=0)

    print('Files merged')
    
    return excl_merged

这里是转换描述列以替换违规字符的代码:

def transform(df, confidence, min_length, operations):
    if "special" in operations.lower():
        df['Description'] = df['Description'].str.replace(r'[\v\n\r\t]+','; ', regex=True)

    # ... Omitted code ...

    return df
python pandas excel
© www.soinside.com 2019 - 2024. All rights reserved.