繁体   English   中英

将文件读入DataFrame时,pandas.read_csv()是否可以删除'\\ s'和'\\ t'char?

[英]Does pandas.read_csv() has the way to remove '\s' and '\t' char when reads the a file into DataFrame?

我需要将.csv文件读入DataFrame,但该文件包含/ s和/ t字符,这些字符会影响进一步的分析。 最好先处理文件并删除\\ s \\ t char吗? 还是pandas.read_csv()有办法做到?

我尝试修改.csv文件,但对我来说似乎不是一个好的解决方案,特别是如果代码在需要获得在CD上写权限的特定环境中运行时,尤其如此。

with open(file, 'r+') as fl:
    stream = str()
    for char in fl.read():
        if char != ' ':
            stream += char
        fl.seek(0)
        fl.flush()
        fl.write(stream)
data = pd.read_csv(file)

对我来说,如果可以在读取(pd.read_csv())或使用pandas.DataFrame()对象中的数据时执行此操作会更好。

到目前为止,我用尽的是将数据加载到DataFrame中,然后调用df.columns = df.columns.str.strip() 我希望这就是你的意思。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM