python - Pandas 忽略作为参数传递的分隔符
问题描述
我正在研究一个功能,在其他任务中,应该读取熊猫中的 csv。作为参数之一,我想将分隔符作为字符串传递。但是,出于某种原因,可能与正则表达式有关,pandas 完全忽略了我传递的解析器并默认为 '\t',它不会正确解析我的数据。
import pandas as pd
def open_df(separator):
df = pd.read_csv('filename.csv', sep=separator)
return df
问题是,在这种情况下我应该如何传递分隔符参数?
解决方案
请检查此链接: https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html
sep : str, 默认 ','</p>
Delimiter to use. If sep is None, the C engine cannot automatically detect the separator, but the Python parsing engine can,
这意味着后者将被 Python 的内置嗅探器工具 csv.Sniffer 使用并自动检测分隔符。此外,长度超过 1 个字符且不同于 '\s+' 的分隔符将被解释为正则表达式,并且还将强制使用 Python 解析引擎。请注意,正则表达式分隔符容易忽略引用的数据。正则表达式示例:'\r\t'。
推荐阅读
- vb.net - 如何在 VB.NET 中为发布版本创建条件编译语句?
- postgresql - 我已经将错误的 psql 转储加载到我的数据库中,无论如何要恢复?
- sql - 查询每个调度的最新序列
- orientdb - 尝试编写包含空格的文本时,API 方法 OClass.setCustom 在分布式环境中失败
- javascript - 猫头鹰轮播,带有 Chrome 的模糊过滤器渲染错误
- flutter - 给定 podspec 的名称“FlutterToast”与预期的“fluttertoast”不匹配
- reactjs - 在另一个函数中使用 useCallback 可以吗?
- java - reddit OAuth2 使用 Jsoup 登录
- python - 如何将 python 对象插入 postgreSQL
- python - 仅具有标称特征的过采样,在这种情况下哪种过采样或欠采样技术可能有效?