1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

簡単なファイル操作をするためのパッケージ filer

1
Posted at

簡単なファイル操作をするためのパッケージ filerの紹介

はじめに

pythonでcsvファイルを読み込む場合、

import csv

with open('name.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        print row

などとする必要があるが、毎回これをやるのはめんどくさい・・・
関数として保存しておけばいいが、パッケージ化すれば導入が楽じゃん!!
ということでpipに登録しました。

導入

2系ならば

pip install filer2

3系ならば

pip install filer3

としてください。(filer2とfiler3で微妙に中身が違うができることは同じ)

使い方

filerで使える関数は

  • read_csv
  • write_csv
  • read_tsv
  • write_tsv
  • read_txt
  • write_txt
  • read_pkl
  • write_pkl
  • conv_encoding

の9種類。
例えばcsvファイルを読み、書き込みは、

from filer2 import Filer

# csvファイルの読み込み
data = Filer.read_csv('inputpath')
# csvファイルの書き込み
Filer.write_csv(data, 'outputpath')

でできる。

同様に、tsvファイル、txtファイル、pklファイルの操作も可能。

conv_encodingは文字のエンコーディングを出力する関数

from filer2 import Filer

print Filer.conv_encoding(string)

として使う。

終わりに

ちょっとしたファイル操作をするとき便利です。
まあ、そもそもpandasのread_csvを使えばいいのですが・・・

余談

久々にpypiの登録を行ったのですが、
現在はどうやら

python setup.py register

とすると、
Server response (410): This API is no longer supported, instead simply upload the file.
というエラーが出ました。
どうやら、setup.pyのregsiterは推奨されていないようです。
今はtwineというパッケージを使って、

twine register dist/*
twine upload dist/*

がいいそうです。
詳しくはこちら
http://qiita.com/tokyo-noctambulist/items/cafcbd589e39d45d693f
http://elicon.blog57.fc2.com/blog-entry-422.html

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?