【数据处理】之读取csv文件报错

本文介绍了解决Python中处理大型CSV文件时遇到的三个常见错误的方法。包括如何调整字段大小限制以避免溢出错误、确保文件以正确模式打开以及转换字符串解码函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错1:OverflowError: Python int too large to convert to C long

在这里插入图片描述

csv.field_size_limit(sys.maxsize)更改为下:

import sys
maxInt = sys.maxsize
decrement = True
while decrement:
    decrement = False
    try:
        csv.field_size_limit(maxInt)
    except OverflowError:
        maxInt = int(maxInt/10)
        decrement = True

报错2:_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)在这里插入图片描述

更改:with open(infile, "r+b") as tsv_in_file:去掉bwith open(infile, "r") as tsv_in_file:

报错3:TypeError: expected bytes-like object, not str

在这里插入图片描述
更改:将base64.decodestring改为base64.b64decode

读取

读取的内容如下:trainval_resnet101_faster_rcnn_genome_36.tsv

{150367: OrderedDict([('image_id', 150367), ('image_w', 640), ('image_h', 480), ('num_boxes', 36), 
('boxes', array([[  0.        , 277.6827    , 583.3728    , 479.2       ],
       [389.56876   , 239.18001   , 475.52667   , 330.16315   ],
       [300.33197   , 265.5234    , 387.11652   , 347.883     ],
       [118.00602   , 138.60199   , 289.6468    , 235.06424   ],
       [  0.        ,   0.        , 252.79614   , 140.41837   ],
       [156.60852   , 352.786     , 344.48508   , 450.08588   ],
       [280.75128   , 146.12346   , 347.84033   , 265.48737   ],
       [386.7497    ,  44.484303  , 522.03094   , 154.81404   ],
       [338.34506   ,   0.        , 639.2       , 164.73306   ],
       [321.5712    , 358.26862   , 377.40723   , 448.2745    ],
       [279.20203   ,   0.        , 584.5702    , 132.85336   ],
       [263.4151    , 366.8589    , 314.9948    , 444.99268   ],
       [440.21368   , 156.93144   , 500.5992    , 193.9488    ],
       [  0.        ,   0.        , 608.7625    , 350.63208   ],
       [452.19727   , 384.70477   , 542.59      , 425.482     ],
       [  0.86831665, 315.65247   ,  70.51543   , 366.01852   ],
       [149.9029    , 152.9564    , 193.36127   , 188.3182    ],
       [ 27.668285  ,   4.043219  , 181.89998   , 120.18465   ],
       [276.54065   ,  54.26455   , 639.2       , 454.42773   ],
       [141.57083   , 140.46927   , 455.4457    , 344.03836   ],
       [584.67584   ,   0.        , 639.2       , 107.73751   ],
       [169.18588   , 359.8093    , 271.23724   , 456.87762   ],
       [158.44717   ,   0.        , 214.74826   , 108.832054  ],
       [  0.        , 302.57465   ,  78.91972   , 395.2979    ],
       [  0.        ,  18.055298  , 286.9795    , 464.57227   ],
       [503.02118   ,  49.59502   , 582.64844   , 156.79427   ],
       [ 75.55986   , 327.47873   , 421.8147    , 463.39697   ],
       [403.1822    ,  65.44243   , 437.54654   , 103.289085  ],
       [277.84735   , 345.26016   , 382.9519    , 455.149     ],
       [306.05084   , 353.73135   , 386.62222   , 454.88385   ],
       [242.39853   , 355.86627   , 333.31012   , 453.9502    ],
       [203.40878   ,   2.5947204 , 379.92694   ,  80.30624   ],
       [263.6872    , 144.34995   , 479.49268   , 319.98187   ],
       [ 26.716516  , 131.85942   , 322.95587   , 259.661     ],
       [ 39.608967  , 170.50273   , 219.11008   , 257.4533    ],
       [ 67.062485  , 213.83627   , 249.61057   , 352.60757   ]],
      dtype=float32)),
   ('features', array([[1.44618011e+00, 1.47522032e+00, 0.00000000e+00, ...,
        4.66062082e-03, 3.41793269e-01, 3.53076506e+00],
       [0.00000000e+00, 3.08756456e-02, 1.28461942e-02, ...,
        0.00000000e+00, 3.06821561e+00, 4.79970551e+00],
       [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,
        0.00000000e+00, 5.00800729e-01, 4.81421280e+00],
       ...,
       [1.80609477e+00, 3.42215180e+00, 5.02800453e-04, ...,
        1.20038791e-02, 5.75995731e+00, 6.30236685e-01],
       [1.04645562e+00, 1.88614297e+00, 1.20871745e-01, ...,
        0.00000000e+00, 7.07430887e+00, 1.54321182e+00],
       [4.25126886e+00, 2.45517805e-01, 2.20697618e+00, ...,
        0.00000000e+00, 7.87591279e-01, 3.96637154e+00]], dtype=float32))])}

Process finished with exit code 0


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值