首页 > 编程知识 正文

python本地读写数据(python读取数据库数据,并保存本地)

时间:2023-12-24 12:05:58 阅读:321325 作者:ZJOX

本文目录一览:

python 读取本地数据然后插入到另一个数据库中

class Buffer(object):

    MAXSIZE = 8192

    def __init__(self, conn, sql):

        self.conn = conn

        self.sql = sql

        self.buffer = []

    def append(self, data):

        self.buffer.append(data)

        if len(self.buffer)  self.MAXSIZE:

            self.flush()

    def flush(self):

        data, self.buffer = self.buffer, []

        curr = self.conn.cursor()

        curr.executemany(self.sql, data)

        self.conn.commit()

# here are your code for init database connect conn_src and conn_store...

buff = Buffer(conn_store, "insert into sybase_user values (%s, %s)")

sql_query = "select a.id, a.name from user_info a where a.id=%s"

curr_src = conn_src.cursor()

curr_src.execute(sql_query, '0001')

for row in curr_src:

    buff.append(row)

buff.flush()

Python3 - 读写字节数据

读写二进制文件,比如图片,声音文件等等。

使用模式为 rb 或 wb 的 open() 函数来读取或写入二进制数据。比如:

在读取二进制数据时,需要指明的是所有返回的数据都是字节字符串格式的,而不是文本字符串。

类似的,在写入的时候,必须保证参数是字节字符串或字节数组对象等。

读取二进制数据时,字节字符串和文本字符串的语义差异可能会导致一个潜在的陷阱。 特别需要注意的是,索引和迭代操作,返回的是字节的值而不是字节字符串。比如:

如果从二进制模式的文件中读取或写入文本数据,必须确保要进行解码和编码操作。比如:

python 读取大文件数据怎么快速读取

python中读取数据的时候有几种方法,无非是read,readline,readlings和xreadlines几种方法,在几种方法中,read和xreadlines可以作为迭代器使用,从而在读取大数据的时候比较有效果.

在测试中,先创建一个大文件,大概1GB左右,使用的程序如下:

[python] view plaincopyprint?

import os.path

import time

while os.path.getsize('messages') 1000000000:

f = open('messages','a')

f.write('this is a file/n')

f.close()

print 'file create complted'

在这里使用循环判断文件的大小,如果大小在1GB左右,那么结束创建文件。--需要花费好几分钟的时间。

测试代码如下:

[python] view plaincopyprint?

#22s

start_time = time.time()

f = open('messages','r')

for i in f:

end_time = time.time()

print end_time - start_time

break

f.close()

#22s

start_time = time.time()

f = open('messages','r')

for i in f.xreadlines():

end_time = time.time()

print end_time - start_time

break

f.close()

start_time = time.time()

f = open('messages','r')

k= f.readlines()

f.close()

end_time = time.time()

print end_time - start_time

使用迭代器的时候,两者的时间是差不多的,内存消耗也不是很多,使用的时间大概在22秒作用

在使用完全读取文件的时候,使用的时间在40s,并且内存消耗相当严重,大概使用了1G的内存。。

其实,在使用跌倒器的时候,如果进行连续操作,进行print或者其他的操作,内存消耗还是不可避免的,但是内存在那个时候是可以释放的,从而使用迭代器可以节省内存,主要是可以释放。

而在使用直接读取所有数据的时候,数据会保留在内存中,是无法释放这个内存的,从而内存卡死也是有可能的。

在使用的时候,最好是直接使用for i in f的方式来使用,在读取的时候,f本身就是一个迭代器,其实也就是f.read方法

如何在python中读写和存储matlab的数据文件

使用sicpy.io即可.sicpy.io提供了两个函数loadmat和savemat,非常方便.

import scipy.io as sio  

import matplotlib.pyplot as plt  

import numpy as np  

#matlab文件名  

matfn=u'E:/python/测试程序/162250671_162251656_1244.mat'  

data=sio.loadmat(matfn)  

plt.close('all')  

xi=data['xi']  

yi=data['yi']  

ui=data['ui']  

vi=data['vi']  

plt.figure(1)  

plt.quiver( xi[::5,::5],yi[::5,::5],ui[::5,::5],vi[::5,::5])  

plt.figure(2)  

plt.contourf(xi,yi,ui)  

plt.show()  

sio.savemat('saveddata.mat', {'xi': xi,'yi': yi,'ui': ui,'vi': vi})

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。