[fortran] 全角空白「 」を2個の半角空白「 」に変換するプログラム部品
全角空白「 」を2個の半角空白「 」に変換するプログラム部品です。
行のコラム数自体は変更しませんので列が崩れたりしないところがポイントです。
データ編集ではこの目に見えない「全角空白」というものが悪さをします。
このプログラム部品を用いて、まず最初に処理対象のデータファイル全体について
全角空白を半角空白に変換しておけば、
データ編集でのエラーを格段に減らすことができます。
応用編として2個ではなく1個の半角空白に変換したいなら
とある部分を書き直せばよいです。
なお、文字コードはWindowsを想定しています。
別のコンピュータの場合にはそれぞれ適切な文字コード表を 用いる必要があります。
!tm -------------------------------------------------------
subroutine chg_fs2hs(ifile, ofile)
!tm 「 」全角空白 -> 「 」半角空白
integer (8), parameter :: in_unit = 9917
integer (8), parameter :: out_unit = 9918
character (len=2), parameter :: moji_to_find = char(129) // char(64) ! full-width space「 」
character (len=2), parameter :: moji_to_replace_with = ' ' ! two half-width spaces「 」
character (len=2) :: moji
character (*) :: ifile, ofile
open (in_unit, file=ifile, access='stream', form='formatted', status='unknown')
open (out_unit, file=ofile, access='stream', form='formatted', status='unknown')
do
read (in_unit, '(a)', advance='no', eor=100, end=110) moji(1:1) ! read the first byte
if (ichar(moji(1:1))>127) then ! assume two-byte character if 1st byte is > 127
read (in_unit, '(a)', advance='no') moji(2:2) ! read the 2nd byte
if (moji==moji_to_find) then
write (out_unit, '(a)', advance='no') moji_to_replace_with ! output replaced char
else
write (out_unit, '(a)', advance='no') moji ! two-byte char output as is
end if
else
write (out_unit, '(a)', advance='no') moji(1:1) ! single byte char output as is
end if
cycle
100 write (out_unit, '()') ! new record
end do
110 continue
close (in_unit)
close (out_unit)
end subroutine chg_fs2hs
行のコラム数自体は変更しませんので列が崩れたりしないところがポイントです。
データ編集ではこの目に見えない「全角空白」というものが悪さをします。
このプログラム部品を用いて、まず最初に処理対象のデータファイル全体について
全角空白を半角空白に変換しておけば、
データ編集でのエラーを格段に減らすことができます。
応用編として2個ではなく1個の半角空白に変換したいなら
とある部分を書き直せばよいです。
なお、文字コードはWindowsを想定しています。
別のコンピュータの場合にはそれぞれ適切な文字コード表を 用いる必要があります。
!tm -------------------------------------------------------
subroutine chg_fs2hs(ifile, ofile)
!tm 「 」全角空白 -> 「 」半角空白
integer (8), parameter :: in_unit = 9917
integer (8), parameter :: out_unit = 9918
character (len=2), parameter :: moji_to_find = char(129) // char(64) ! full-width space「 」
character (len=2), parameter :: moji_to_replace_with = ' ' ! two half-width spaces「 」
character (len=2) :: moji
character (*) :: ifile, ofile
open (in_unit, file=ifile, access='stream', form='formatted', status='unknown')
open (out_unit, file=ofile, access='stream', form='formatted', status='unknown')
do
read (in_unit, '(a)', advance='no', eor=100, end=110) moji(1:1) ! read the first byte
if (ichar(moji(1:1))>127) then ! assume two-byte character if 1st byte is > 127
read (in_unit, '(a)', advance='no') moji(2:2) ! read the 2nd byte
if (moji==moji_to_find) then
write (out_unit, '(a)', advance='no') moji_to_replace_with ! output replaced char
else
write (out_unit, '(a)', advance='no') moji ! two-byte char output as is
end if
else
write (out_unit, '(a)', advance='no') moji(1:1) ! single byte char output as is
end if
cycle
100 write (out_unit, '()') ! new record
end do
110 continue
close (in_unit)
close (out_unit)
end subroutine chg_fs2hs