datastore 関数で日本語文字読み込むことは可能ですか?

7 vues (au cours des 30 derniers jours)
MathWorks Support Team
MathWorks Support Team le 22 Août 2017
現在、添付のような csv 形式のファイルを datastore 関数を使用して読み込んだところ、文字化けが発生しました。対策はありますか。
 
ds=datastore('ja_sjis.csv')
ds.read

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 14 Fév 2018
関数 datastore は Shift_JIS 文字コードでエンコードされたファイルは正しく解読できません。
datastore 関数(特に TabularTextDatastore)では、原則 ASCII もしくは UTF-8 のエンコードをサポートしており、メモリに十分余裕がある場合のみ Shift_JIS エンコードをサポートしています。
対策として、ASCII または UTF-8 の csv ファイルをご利用ください。
日本語文字が含まれている場合、下記関数 (convertEncoding.m) を用いて文字コード UTF-8 に変換が可能です。
 
convertEncoding('ja_sjis.csv', 'Shift_JIS', 'UTF-8');
ds = datastore('ja_sjis.csv')
ds.read
 
% convertEncoding.m
%----------------------------------------------------------------------
%CONVERTENCODING Change the character encoding of a file
% CONVERTENCODING(FILENAME, CURRENTENCODING, NEWENCODING) converts the
% file FILENAME from character encoding CURRENTENCODING to a new
% encoding specified by NEWENCODING. A copy of the original version of
% FILENAME is placed at FILENAME.old.
%
% INPUT PARAMETERS:
% FILENAME: The name of the file to be converted.
% CURRENTENCODING: The encoding currently be used by FILENAME.
% NEWENCODING: The encoding to rewrite FILENAME in.
function convertEncoding(filename, currentEncoding, newEncoding)
bakFile = [filename, '.old'];
movefile(filename, bakFile);
fpIn = fopen(bakFile, 'r', 'n', currentEncoding);
fpOut = fopen(filename, 'w', 'n', newEncoding);
while feof(fpIn) == 0
lineIn = fgets(fpIn);
fwrite(fpOut, lineIn, 'char');
end
fclose(fpIn);
fclose(fpOut);
end
%----------------------------------------------------------------------
 

Plus de réponses (0)

Tags

Aucun tag saisi pour le moment.

Produits


Version

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!