really simple csv importer を使うために csv をutf-8に変換するために nkf をインストールするために homebrew をインストールするために xcode をアップデート中。。。

wordpressでECサイトを構築する案件。
300件近くある商品情報を一括で記事として投稿する方法を探したところ、
really simple csv importer が良いらしいのですが、
試してみたところ、なぜかタイトルも記事も読み込まれません。


全部”タイトルなし”に。。。

どうやら文字コードの問題のようですので、csvファイルをutf-8に変換しようとするもlibreoffice のスプレッドシートでは無理な模様。
windowsだったら秀丸エディタでできてたのに。。。

フリーの良いエディタは無いかなと思って探していると nkf というコマンドがあるそうな。

コマンドラインから
nkf -w [入力ファイル名] > [出力ファイル名]
でutf-8に変換できるそうなのですが、インストールされておらず。

macのパッケージ管理システムも入ってないみたいで homebrew というのをインストールすることにする。

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /usr/local
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 203632, done.
remote: Compressing objects: 100% (54818/54818), done.
remote: Total 203632 (delta 147629), reused 203632 (delta 147629)
Receiving objects: 100% (203632/203632), 43.29 MiB | 90.00 KiB/s, done.
Resolving deltas: 100% (147629/147629), done.
From https://github.com/Homebrew/homebrew
* [new branch]      master     -> origin/master
HEAD is now at 61eb490 hashpump: add 1.1.0_1 bottle.
==> Installation successful!
==> Next steps
Run `brew doctor` before you install anything
Run `brew help` to get started

で言われた通りbrew doctorすると、

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: Your Xcode (5.1.1) is outdated
Please update to Xcode 6.0.1.
Xcode can be updated from the App Store.
$ 

なんかXcodeバージョンが古いそうなので今アップデート中。。。

再訪問に行って帰って来ると、

$ brew doctor
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!
Warning: Git could not be found in your PATH.
Homebrew uses Git for several internal functions, and some formulae use Git
checkouts instead of stable tarballs. You may want to install Git:
brew install git
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
Warning: You have not agreed to the Xcode license.
Builds will fail! Agree to the license by opening Xcode.app or running:
xcodebuild -license
$ sudo brew help
Password:
Example usage:
brew [info | home | options ] [FORMULA...]
brew install FORMULA...
brew uninstall FORMULA...
brew search [foo]
brew list [FORMULA...]
brew update
brew upgrade [FORMULA...]
brew pin/unpin [FORMULA...]
Troubleshooting:
brew doctor
brew install -vd FORMULA
brew [--env | config]
Brewing:
brew create [URL [--no-fetch]]
brew edit [FORMULA...]
open https://github.com/Homebrew/homebrew/wiki/Formula-Cookbook
Further help:
man brew
brew home
$

と、どうやらbrew はインストールされた模様。
さっそくnkfをインストールしようとすると、

$ brew install nkf
Warning: You have not agreed to the Xcode license.
Builds will fail! Agree to the license by opening Xcode.app or running:
xcodebuild -license
Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.

なんかXcode利用規約にagreeしないといけないとか。
Xcodeを立ち上げる→利用規約に同意する→閉じる
でもう一回インストール

$ brew install nkf
==> Downloading http://dl.sourceforge.jp/nkf/59912/nkf-2.1.3.tar.gz
Already downloaded: /Library/Caches/Homebrew/nkf-2.1.3.tar.gz
==> make CC=clang
==> make install prefix=/usr/local/Cellar/nkf/2.1.3 MKDIR=mkdir -p
/usr/local/Cellar/nkf/2.1.3: 4 files, 352K, built in 2 seconds
$ nkf --version
Network Kanji Filter Version 2.1.3 (2013-11-22)
Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa).
Copyright (C) 1996-2013, The nkf Project.

と、どうやらインストールされた感じ。
nkf は Network Kanji Filter の略なんですね〜

で、今回のcsvファイルの文字コードを見ると

$ nkf -g testdata1_50.csv
Shift_JIS

これが原因?
さっそく変換してみます。

$ nkf -w testdata1_50.csv > testdata1_50utf8.csv
$ nkf -g testdata1_50.csv
Shift_JIS
$ nkf -g testdata1_50utf8.csv
UTF-8

再度 Really Simple CSV Importer から読み込ませると。。。
ブジ登録されてました!!
よかった〜

オプションは

-s Shift_JISへ変換
-w UTF-8へ変換
-e EUCへ変換

ちなみにEUC-JPは確認するとCP51932と表示されます 

コメント

タイトルとURLをコピーしました