读取带有垃圾值的错误的csv文件 [英] Reading bad csv files with garbage values

查看:136
本文介绍了读取带有垃圾值的错误的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用熊猫读取具有以下格式的csv文件:

I wish to read a csv file which has the following format using pandas:

    atrrth
    sfkjbgksjg
    airuqghlerig
    Name         Roll
    airuqgorqowi
    awlrkgjabgwl
    AAA          67
    BBB          55
    CCC          07

如您所见,如果我使用pd.read_csv,则会收到相当明显的错误:

As you can see, if I use pd.read_csv, I get the fairly obvious error:

 ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2

但是我希望将整个数据放入一个数据帧中.使用error_bad_lines = False将删除重要内容,仅保留垃圾值

But I wish to get the entire data into a dataframe. Using error_bad_lines = False will remove the important stuff and leave only the garbage values

这些是可能的列名称中的2个,如下所示:

These are the 2 of the possible column names as given below :

Name : [Name , NAME , Name of student] 
Roll : [Rollno , Roll , ROLL]

如何实现?

推荐答案

打开csv文件,并从列名的开头查找一行:

Open the csv file and find a row from where the column name starts:

with open(r'data.csv') as fp:
    skip = next(filter(
        lambda x: x[1].startswith(('Name','NAME')),
        enumerate(fp)
    ))[0]

该值将存储在skip参数

import pandas as pd
df = pd.read_csv('data.csv', skiprows=skip)

在Python 3.X中工作

Works in Python 3.X

这篇关于读取带有垃圾值的错误的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆