代码之家  ›  专栏  ›  技术社区  ›  user4462740

使用pandas read_csv读取这个由制表符分隔的文件时,会丢失行

  •  10
  • user4462740  · 技术社区  · 9 年前

    我有一个 .text 文件格式如下,其中字段(索引号、名称和消息)由 \t (制表符分隔):

    712 ben     Battle of the Books
    713 james   i used to be in TOM
    714 tomy    i was in BOB once
    715 ben Tournaments of Minds
    716 tommy    Also the Lion in the upcoming school play
    717 tommy   Can you guess
    718 tommy    P
    ...
    

    我用它读 read_csv 转换成数据帧:

     chat = pd.read_csv("f.text", sep = "\t", header = None, usecols = [2])
    

    但数据帧只有 9812 行,而普通文件的行数超过 12428 行(只有21个空行)。这很奇怪。你有什么想法吗?谢谢

    1 回复  |  直到 5 年前
        1
  •  16
  •   jezrael    9 年前

    我想你需要添加参数 quoting :

    import csv
    
    chat = pd.read_csv("f.text",sep = "\t", header = None, usecols = [2], quoting=csv.QUOTE_NONE)