rainit2006 / Python-room

my python room
0 stars 0 forks source link

文件处理 #16

Open rainit2006 opened 6 years ago

rainit2006 commented 6 years ago

基本I/O处理 http://www.runoob.com/python/python-files-io.html

rainit2006 commented 6 years ago

打开一个文件

fo = open("foo.txt", "w") print "文件名: ", fo.name print "是否已关闭 : ", fo.closed print "访问模式 : ", fo.mode print "末尾是否强制加空格 : ", fo.softspace


写文件用write函数

打开一个文件

fo = open("foo.txt", "w") fo.write( "www.runoob.com!\nVery good site!\n")

关闭打开的文件

fo.close()

读文件用read

打开一个文件

fo = open("foo.txt", "r+") str = fo.read(10) print "读取的字符串是 : ", str

关闭打开的文件

fo.close()



- 文件定位
tell()方法告诉你文件内的当前位置.
seek(offset [,from])方法改变当前文件的位置。

- 重命名和删除文件
os.rename(current_file_name, new_file_name)
os.remove(file_name)

- 创建新的目录
os.mkdir("newdir")

- 改变当前的目录
os.chdir("newdir")

- 显示当前的工作目录
os.getcwd()
- 删除目录
os.rmdir('dirname')
rainit2006 commented 6 years ago

文件处理方法: http://www.runoob.com/python/file-methods.html

rainit2006 commented 6 years ago

读取大文件 https://chenqx.github.io/2014/10/29/Python-fastest-way-to-read-a-large-file/ https://github.com/Shuang0420/Shuang0420.github.io/wiki/python%E8%AF%BBGB%E7%BA%A7%E5%A4%A7%E6%96%87%E4%BB%B6

思路: 调用read()会一次性读取文件的全部内容,如果文件有10G,内存就爆了,所以,要保险起见,可以反复调用read(size)方法。 如果文件很小,read()一次性读取最方便;如果不能确定文件大小,反复调用read(size)比较保险;如果是配置文件,调用readlines()最方便。

首推with语句 with语句打开和关闭文件,包括抛出一个内部块异常。for line in f文件对象f视为一个迭代器,会自动的采用缓冲IO和内存管理,所以你不必担心大文件。

#If the file is line based
with open(...) as f:
    for line in f:
        process(line) # <do something with line>

或者Read In Chunks 处理大文件是很容易想到的就是将大文件分割成若干小文件处理,处理完每个小文件后释放该部分内存。这里用了 iter & yield:

def read_in_chunks(filePath, chunk_size=1024*1024):
    """
    Lazy function (generator) to read a file piece by piece.
    Default chunk size: 1M
    You can set your own chunk size 
    """
    file_object = open(filePath)
    while True:
        chunk_data = file_object.read(chunk_size)
        if not chunk_data:
            break
        yield chunk_data
if __name__ == "__main__":
    filePath = './path/filename'
    for chunk in read_in_chunks(filePath):
        process(chunk) # <do something with chunk>

The WITH statement when used with open file guarantees that the file object is closed when the with block exits.

rainit2006 commented 6 years ago

os库 http://nonbiri-tereka.hatenablog.com/entry/2014/03/14/195241

import os os.getcwd 現在のカレントディレクトリを表示 os.dirname ディレクトリを表示 os.chdir ディレクトリを移動する os.mkdir ディレクトリを作る os.listdir ディレクトリの中を全てリストにする os.path.isdir ディレクトリかどうか。あればTrue,なければFalse

os.path.join | パスをくっつける os.path.exists | ファイルの有無 os.path.basename | 最も最下層にあるファイルの名前かディレクトリの名前 os.path.abspath | 絶対パスを返す

rainit2006 commented 6 years ago

glob Module https://blog.csdn.net/csapr1987/article/details/7469769 glob模块是最简单的模块之一,内容非常少。用它可以查找符合特定规则的文件路径名。跟使用windows下的文件搜索差不多。查找文件只用到三个匹配符:"", "?", "[]"。""匹配0个或多个字符;"?"匹配单个字符;"[]"匹配指定范围内的字符,如:[0-9]匹配数字。

■glob.glob 返回所有匹配的文件路径列表。

import glob  

#获取指定目录下的所有图片  
print glob.glob(r"E:\Picture\*\*.jpg")  

#获取上级目录的所有.py文件  
print glob.glob(r'../*.py') #相对路径  

■glob.iglob 获取一个可编历对象,使用它可以逐个获取匹配的文件路径名。与glob.glob()的区别是:glob.glob同时获取所有的匹配路径,而 glob.iglob一次只获取一个匹配路径。

import glob  

#父目录中的.py文件  
f = glob.iglob(r'../*.py')  

print f #<generator object iglob at 0x00B9FF80>  

for py in f:  
    print py  

Sample:

import os, glob

os.chdir("C:\\Users\\xxxx\\Documents\\Project\\Python\\JupiterNotebook")
for file in glob.glob("*.ipynb"):
    print(file)
rainit2006 commented 5 years ago

In Python, how do I read a file line-by-line into a list? https://stackoverflow.com/questions/3277503/in-python-how-do-i-read-a-file-line-by-line-into-a-list

with open(fname) as f:
    content = f.readlines()

But, In the case you are working with Big Data using readlines() is not very efficient as it can result in MemoryError. In this case it is better to iterate over the file using for line in f: and working with each line variable. Like below lines = [line.rstrip('\n') for line in file]

rainit2006 commented 5 years ago

pandas でメモリに乗らない 大容量ファイルを上手に扱う http://sinhrks.hatenablog.com/entry/2014/11/21/231534

  1. chunksize を使って ファイルを分割して読み込む read_csv に chunksize オプションを指定することでファイルの中身を 指定した行数で分割して読み込むことができる。chunksize には 1回で読み取りたい行数を指定する。例えば 50 行ずつ読み取るなら、chunksize=50。 reader = pd.read_csv(fname, skiprows=[0, 1], chunksize=50) chunksize を指定したとき、返り値は DataFrame ではなく TextFileReader インスタンスとなる。 TextFileReader を for でループさせると、ファイルの中身を指定した行数ごとに DataFrame として読み取る。TextFileReader は 現時点での読み取り位置 (ポインタ) を覚えており、ファイルの中身をすべて読み取るとループが終了する。
    for r in reader:
    print(type(r), r.shape)

    もしくは、TextFileReader.get_chunk で、現在の読み取り位置 (ポインタ) から N 行を読み取る。

    
    # 先頭から 5行を読み込み
    reader.get_chunk(5)

次の4行 (6行目 - 9行目)を読み込み

reader.get_chunk(4)

分析に必要なレコードは 全データのごく一部、なんてこともよくある。chunk させて読み込んだ 各グループに対して前処理をかけると、サイズが劇的に減ってメモリに乗る。そんなときは、前処理をかけた後、各 DataFrame を結合してひとつにしたい。

DataFrame の結合には pd.concat。TextFileReader から読み込んだ 各 DataFrame はそれぞれが 0 から始まる index を持つ。pd.concat は既定では index を維持して DataFrame を結合するため、そのままでは index が重複してしまう。 重複を避けるためには、結合後に index を振りなおす = ignore_index=True を指定する必要がある。

def preprocess(x):

不要な行, 列のフィルタなど、データサイズを削減する処理

return x

reader = pd.read_csv(fname, skiprows=[0, 1], chunksize=50) df = pd.concat((preprocess(r) for r in reader), ignore_index=True)

df.shape

(258, 59)