<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Yukun&#039;s Blog &#187; Character</title>
	<atom:link href="http://www.yukun.info/blog/tag/character/feed" rel="self" type="application/rss+xml" />
	<link>http://www.yukun.info</link>
	<description>難しいことは分かりやすく、簡単なことは面白く紹介</description>
	<lastBuildDate>Thu, 26 Jan 2012 03:33:59 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python: リスト中の文字列を大文字⇔小文字に変換</title>
		<link>http://www.yukun.info/blog/2008/08/python-upper-lower-case-letters-converted.html</link>
		<comments>http://www.yukun.info/blog/2008/08/python-upper-lower-case-letters-converted.html#comments</comments>
		<pubDate>Sat, 02 Aug 2008 15:00:38 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Character]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[Loop]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://trumpcode.yukun.info/?p=247</guid>
		<description><![CDATA[文字列を比較する際に、大文字・小文字を区別したくない場合があります。その時は、比較する文字列を大/小文字列のどちらかに統一しておく、という手があります。Pythonでは大文字・小文字変換メソッドlower()、upper &#8230; <a href="http://www.yukun.info/blog/2008/08/python-upper-lower-case-letters-converted.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/08/python-upper-lower-case-letters-converted.html">Python: リスト中の文字列を大文字⇔小文字に変換</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>文字列を比較する際に、大文字・小文字を区別したくない場合があります。その時は、比較する文字列を大/小文字列のどちらかに統一しておく、という手があります。Pythonでは大文字・小文字変換メソッドlower()、upper()はstringオブジェクトに組み込まれています。<br />
今回は、その使い方と実際に使用する状況に近いデータ構造、ここでは変換対象文字列がリスト中の要素である場合を想定し、for文とリストコンプリヘンション(リスト内包表記)の両表記を以下に示します。</p>
<h3>ソースコード</h3>
<pre>
#!/usr/bin/python
# coding: UTF-8
# リスト中の文字列要素を大文字⇔小文字変換

str_atog = "ABCDEFG"
str_hton = "hijklmn"

# lower(), upper()メソッドの使い方
print "大文字(列) %s を小文字(列) %s に変換" % (str_atog, str_atog.lower())
print "小文字(列) %s を大文字(列) %s に変換" % (str_hton, str_hton.upper())
print

arr  = ['And', 'Begin', 'Code', 'Double']
arr2 = ['end', 'flag', 'gem', 'halt']
# for文で小文字[大文字](列)を要素とするリストを生成
n_arr = []
for str in arr:
    n_arr.append(str.lower())
print n_arr

n_arr2 = []
for str in arr2:
    n_arr2.append(str.upper())
print n_arr2, 'n'

# リストコンプリヘンションで小文字[大文字](列)を要素とするリストを生成
print [str.lower() for str in arr]
print [str.upper() for str in arr2]
</pre>
<h3>実行結果</h3>
<pre>
大文字(列) ABCDEFG を小文字(列) abcdefg に変換
小文字(列) hijklmn を大文字(列) HIJKLMN に変換

['and', 'begin', 'code', 'double']
['END', 'FLAG', 'GEM', 'HALT']

['and', 'begin', 'code', 'double']
['END', 'FLAG', 'GEM', 'HALT']
</pre>
<p>List Comprehensions なら<strong>ワンライナー</strong>で書けるってのは地味に良いですね。<br />
話変わりますが、Rubyにもイテレータやブロックを用いた簡略記法がありましたね。アレはアレで、応用しやすいものです。</p>
<h3> チュートリアル</h3>
<ul>
<li><a href="http://docs.python.org/tut/node7.html" target="_blank">5. Data Structures</a>
<ul>
<li><a href="http://docs.python.org/tut/node7.html#SECTION007140000000000000000" target="_blank">5.1.4 List Comprehensions</a></li>
</ul>
</li>
</ul>
<h3>リファレンス</h3>
<ul>
<li><a href="http://docs.python.org/lib/module-string.html" title="4.1 string -- Common string operations" target="_blank">4.1 string &#8212; Common string operations</a>
<ul>
<li><a href="http://docs.python.org/lib/node39.html" title="4.1.1 String constants" target="_blank">4.1.1 String constants</a></li>
<li><a href="http://docs.python.org/lib/node42.html" title="4.1.4 Deprecated string functions" target="_blank">4.1.4 Deprecated string functions</a></li>
</ul>
</li>
</ul>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/06/python-list-comprehension.html" rel="bookmark" title="2008年6月16日">Python: リスト内包表記(リストコンプリヘンション)をfor文に書き換える</a></li>
<li><a href="http://www.yukun.info/blog/2008/08/python-sequence-string.html" rel="bookmark" title="2008年8月6日">Python: 文字列の上位型であるシーケンス型の構文 &#8211; Sequence[X:Y:Z]</a></li>
<li><a href="http://www.yukun.info/blog/2008/08/python-if-for-in.html" rel="bookmark" title="2008年8月2日">Python: if/for文でのin演算子の各オブジェクト毎の評価</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-sequence.html" rel="bookmark" title="2008年6月2日">Python: リストの抽出・連結・要素の追加</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/python-list.html" rel="bookmark" title="2008年6月1日">Python: リストの初期化・出力・代入・要素数</a></li>
</ul>
<p><!-- Similar Posts took 8.709 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/08/python-upper-lower-case-letters-converted.html">Python: リスト中の文字列を大文字⇔小文字に変換</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/08/python-upper-lower-case-letters-converted.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linuxコマンドで複数ファイルの文字コードを一括変換</title>
		<link>http://www.yukun.info/blog/2008/01/euc-to-utf8.html</link>
		<comments>http://www.yukun.info/blog/2008/01/euc-to-utf8.html#comments</comments>
		<pubDate>Fri, 11 Jan 2008 15:00:00 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Character]]></category>
		<category><![CDATA[Command]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/19700101/linux%e3%82%b3%e3%83%9e%e3%83%b3%e3%83%89%e3%81%a7%e8%a4%87%e6%95%b0%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%81%ae%e6%96%87%e5%ad%97%e3%82%b3%e3%83%bc%e3%83%89%e3%82%92%e4%b8%80%e6%8b%ac%e5%a4%89</guid>
		<description><![CDATA[Linux系OSのfedora6のデフォルト文字コードはUTF8なので、先日久々に参照したEUCのC++ソースコード中のコメントや出力が文字化けしていました。 そこで、ファイルの文字コードをEUCからUTF8に変換するコ &#8230; <a href="http://www.yukun.info/blog/2008/01/euc-to-utf8.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/01/euc-to-utf8.html">Linuxコマンドで複数ファイルの文字コードを一括変換</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Linux系OSのfedora6のデフォルト文字コードはUTF8なので、先日久々に参照したEUCのC++ソースコード中のコメントや出力が文字化けしていました。</p>
<p>そこで、ファイルの文字コードをEUCからUTF8に変換するコマンドを調べたところ、<a href="http://www.phppro.jp/phptips/archives/vol30/">PHPプロ！TIPS+</a>のページの中程にそれに関するコマンドがあったので参考にしました。</p>
<pre>$find -name '*.cc' | xargs nkf --overwrite -w</pre>
<p>↑は拡張子がccの全てのテキストファイルの文字コードをutf8に変換します。</p>
<pre>$find . -type f -print0 | xargs -0 nkf --overwrite -w -Lu</pre>
<p>↑このコマンドの意味を簡単に示しますと、まずファイルを検索するfindコマンドで、カレントディレクトリ「.」から通常ファイル「-type f」を探索し出力します「-print0」(常に真)。</p>
<blockquote><p>% find [検索開始ディレクトリ] (option)<br />
<span style="font-style:italic;">参考:<a href="http://www.k-tanaka.net/unix/find.html">UNIXコマンド [find]</a></span></p></blockquote>
<p>ここで、findコマンドの結果をパイプ「|」をもって渡し、そこでxargsでコマンドを実行します。ここでxargsは以下の機能を持ちます。</p>
<blockquote><p>xargs[えっくす・あーぐす]<br />
標準入力から引数を読み込み、指定のコマンドを実行するコマンド<br />
<span style="font-style:italic;">参考:<a href="http://x68000.q-e-d.net/~68user/unix/pickup?xargs">UNIXの部屋 検索:xargs (*BSD/Linux/Solaris)</a></span></p></blockquote>
<p>文字コード変換コマンドである nkf のオプション&#8211;overwriteは変換した文字コードのデータを元のファイルに上書きするもので、-wが文字コードをUTF8に指定するものです。ちなみに、EUCに変換したい場合は-e、Windowsで使われているSJISにする場合は-sを代わりに指定します。</p>
<p>最後の-Luオプションは改行コードをLFに指定するものです。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/06/linux-users.html" rel="bookmark" title="2008年6月26日">ユーザー管理に関するLinuxコマンド</a></li>
<li><a href="http://www.yukun.info/blog/2008/06/sshd-log-file.html" rel="bookmark" title="2008年6月24日">sshdのログファイルの確認方法</a></li>
<li><a href="http://www.yukun.info/blog/2008/12/how-to-write-apache-conf.html" rel="bookmark" title="2008年12月25日">Apacheでよく使うコマンドと設定項目</a></li>
<li><a href="http://www.yukun.info/blog/2008/09/fedora-yum-install-gnome.html" rel="bookmark" title="2008年9月15日">FedoraにGUI環境GNOMEをyumでインストール</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html" rel="bookmark" title="2008年1月5日">Rubyで文字列から日本語文字をインデックス指定する</a></li>
</ul>
<p><!-- Similar Posts took 11.654 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/01/euc-to-utf8.html">Linuxコマンドで複数ファイルの文字コードを一括変換</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/01/euc-to-utf8.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rubyで文字列から日本語文字をインデックス指定する</title>
		<link>http://www.yukun.info/blog/2008/01/ruby-string-index.html</link>
		<comments>http://www.yukun.info/blog/2008/01/ruby-string-index.html#comments</comments>
		<pubDate>Sat, 05 Jan 2008 09:23:17 +0000</pubDate>
		<dc:creator>yukun</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Character]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.yukun.info/trump/20080105/ruby%e3%81%a7%e6%96%87%e5%ad%97%e5%88%97%e3%81%8b%e3%82%89%e6%97%a5%e6%9c%ac%e8%aa%9e%e6%96%87%e5%ad%97%e3%82%92%e3%82%a4%e3%83%b3%e3%83%87%e3%83%83%e3%82%af%e3%82%b9%e6%8c%87%e5%ae%9a%e3%81%99</guid>
		<description><![CDATA[RubyのStringインスタンスに格納されている文字列のインデックスを得るにはchrメソッドを用います。 ソースコード # chrは文字コードObjを文字列Objに変換するメソッド str1 = "abcdef" p  &#8230; <a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html">Continue reading <span class="meta-nav">&#8594;</span></a><p><a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html">Rubyで文字列から日本語文字をインデックス指定する</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>RubyのStringインスタンスに格納されている文字列のインデックスを得るにはchrメソッドを用います。</p>
<h4>ソースコード</h4>
<pre>
# chrは文字コードObjを文字列Objに変換するメソッド
str1 = "abcdef"
p str1[2]     #=> 99
p str1[2].chr #=> "c"
</pre>
<p><span id="more-11"></span></p>
<h3>インデックスはバイト換算</h3>
<p>なので、2バイト長の日本語文字などは取り出せません。</p>
<pre>
str2 = "あいうえお"
p    str2[2]                   #=> 130
puts str2[2]                   #=> 130
p    str2[2].chr               #=> "\202"
puts str2[2].chr + str2[3].chr #=> "い"
</pre>
<p>そこで、</p>
<h3>日本語文字列のインデックス指定はsplitメソッドで一旦配列に分割</h3>
<pre>
str2  = "あいうえお"
jarr3 = str2.split(//s) # 文字コードがSJISの場合
puts  jarr3[3] #=> え
</pre>
<p>多バイト長の文字列処理を扱ったソースを読み込んで慣れていこうかな。</p>
<h4>関連すると思われる記事：</h4>
<ul class="similar-posts">
<li><a href="http://www.yukun.info/blog/2008/08/python-upper-lower-case-letters-converted.html" rel="bookmark" title="2008年8月3日">Python: リスト中の文字列を大文字⇔小文字に変換</a></li>
<li><a href="http://www.yukun.info/blog/2008/02/extract-web-tag.html" rel="bookmark" title="2008年2月10日">Webページから指定したタグの要素を抜き出すRuby関数</a></li>
<li><a href="http://www.yukun.info/blog/2008/02/ruby-string.html" rel="bookmark" title="2008年2月6日">Rubyで引数の設定値によって4パターンの部分文字列を取得するラッパー関数</a></li>
<li><a href="http://www.yukun.info/blog/2008/03/diff-java-ruby-string.html" rel="bookmark" title="2008年3月4日">JavaとRubyで文字列の終端の扱いの違い</a></li>
<li><a href="http://www.yukun.info/blog/2008/01/ruby-block.html" rel="bookmark" title="2008年1月9日">Ruby: メソッドの引数にブロックを渡す</a></li>
</ul>
<p><!-- Similar Posts took 10.359 ms --></p>
<p><a href="http://www.yukun.info/blog/2008/01/ruby-string-index.html">Rubyで文字列から日本語文字をインデックス指定する</a> is a post from: <a href="http://www.yukun.info">Yukun&#039;s Blog</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.yukun.info/blog/2008/01/ruby-string-index.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

