0
1

More than 3 years have passed since last update.

FC2ブログのエクスポートからCSVファイルを作る

Posted at

簡単に組んだスクリプトのメモです。

FC2 ブログのエクスポートは、下記のような形で出力されます。

AUTHOR: sou (08thse)
TITLE: とりあえず
STATUS: Publish
ALLOW COMMENTS: 1
CONVERT BREAKS: default
ALLOW PINGS: 1
PRIMARY CATEGORY: 日記
CATEGORY: 日記

DATE: 07/27/2008 22:01:09
-----
BODY:
ほげほげ
-----
EXTENDED BODY:

-----
EXCERPT:

-----
KEYWORDS:

-----

# 以下、記事ごとに繰り返し

ここから、「タイトル」「カテゴリ」「公開日」を CSV で出力したかったので、サクッと Python で組んでみました。

import pandas as pd

cols = ['title', 'category', 'publish_date']
df = pd.DataFrame(index=[], columns=cols)

with open('export_text.txt', encoding='utf-8') as f:

    while True:
        line = f.readline()
        if line == '':
            break
        if 'TITLE:' in line:
            current_title = line[7:][:-1]
        if 'PRIMARY CATEGORY:' in line:
            current_category = line[18:][:-1]
        if 'DATE:' in line:
            current_publish_date = line[6:][:-1]
            df = df.append({'title': current_title, 'category': current_category, 'publish_date': current_publish_date}, ignore_index=True)

df.to_csv('export_csv.csv')

記事毎の属性の順序は抜け漏れなく決まっているため、順序的に今回は「公開日」まで到達すれば df.append() して OK ということで、こんな感じのロジックにしています。

また、line の最後に改行文字が含まれるので、[:-1] で除外しています。

出力される CSV ファイルは UTF-8 でエンコードされているので、文字化けした際にはエンコーディングに注意しましょう。

0
1
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
0
1