Python extracts millions of data to a CSV file

Source: Internet
Author: User

Transferred from: http://www.2cto.com/kf/201311/258112.html

Today there is a need for the system all user registration ID and mailbox and other information exported to provide them, in MySQL count down, about 3.5 million

On the attempt to use Python implementation, incidentally, the function of Python to write CSV, originally wanted to use the tool, but think the next speed will be very slow, the whole export process is about 3 minutes or so, or pretty fast, after all, there are more than 3 million, after the guide is about 150M below is my script deal_csv.py, Because the MySQL database needs to be connected, the script relies on the MySQLdb module?
123456789101112131415161718192021222324252627282930313233343536373839404142 __author__ = ‘chunyang.wu‘# -*- coding: utf-8 -*-  #!/usr/bin/env python  import MySQLdb  import os  os.environ[‘NLS_LANG‘] = ‘SIMPLIFIED CHINESE_CHINA.UTF8‘import sys  reload(sys)  sys.setdefaultencoding(‘utf-8‘import csv     class Handle:      def __init_db(self):          self._mysql_db = MySQLdb.connect(host="172.16.1.55",user="test",passwd="123456",port=3306,db="test",unix_socket="/tmp/mysql5.sock"        self.mysql_cur=self._mysql_db.cursor()          self.seq = 0      def __init__(self):          self.__init_db()        def _release_db(self):          self.mysql_cur.close()          self._mysql_db.close()         def _do(self):          self.mysql_cur.arraysize = 50        select_sql = "SELECT id,email,FROM_UNIXTIME(create_time) AS create_time FROM test.tbl_member "         print select_sql          self.mysql_cur.execute(select_sql)          count = 0        csvfile = file(‘all_user.csv‘, ‘wb‘        print dir(csv)          writers = csv.writer(csvfile)          writers.writerow([‘uid‘, ‘email‘, ‘createtime‘])          while 1            lines = self.mysql_cur.fetchmany(50            if len(lines)==0                break            for i in lines:                  print                 writers.writerows([i])          csvfile.close() 

def main (): P = Handle () p._do () p._release_db () if __name__== "__main__": Main () CSV file structure as

Python extracts millions of data to a CSV file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.