Python读取大容量的csv文件

python按行遍历一个大文件:

with open('filename') as file:
    for line in file:
        do_things(line)

结合下文的block,读几行是没压力了,但存成csv的时候各种奇葩问题,什么str和byte的编码问题啦,什么csv一打开是各种奇葩的整数啦……


如何用python处理非常大的csv和xml
http://lethain.com/handling-very-large-csv-and-xml-files-in-python/

最后,结合另一个地方看的block控制行数,正常的csv终于可以出来了~~

with open('tianchi_mobile_recommend_train_user.csv', 'r') as fin:
        with open('user-10000.csv','w') as fout:
            block = []
            for line in fin:
                block.append(line)
                if len(block) <= 10:
                        fout.write(','.join(line.split(' ')))
                else:
                      break
            print (block)

你可能感兴趣的:(机器学习,Python数据分析技巧)