1

最初から。

次のような csv ファイルにデータがあります。

La Loi des rues,/m/0gw3lmk,/m/0gw1pvm
L'Étudiante,/m/0j9vjq5,/m/0h6hft_
The Kid From Borneo,/m/04lrdnn,/m/04lrdnt,/m/04lrdn5,/m/04lrdnh,/m/04lrdnb

これは UTF-8 形式です。このファイルを次のようにインポートします(別の場所から取得):

feature('DefaultCharacterSet','UTF-8');
fid = fopen(filename,'rt');         %# Open the file
  lineArray = cell(100,1);          %# Preallocate a cell array (ideally slightly
                                    %# larger than is needed)
  lineIndex = 1;                    %# Index of cell to place the next line in
  nextLine = fgetl(fid);            %# Read the first line from the file
  while ~isequal(nextLine,-1)       %# Loop while not at the end of the file
  lineArray{lineIndex} = nextLine;  %# Add the line to the cell array
  lineIndex = lineIndex+1;          %# Increment the line index
  nextLine = fgetl(fid);            %# Read the next line from the file
end
fclose(fid);                        %# Close the file

これにより、UTF-8 テキストを含む配列が作成されます。{3x1} 配列:

'La Loi des rues,/m/0gw3lmk,/m/0gw1pvm'
'L''Étudiante,/m/0j9vjq5,/m/0h6hft_'
'The Kid From Borneo,/m/04lrdnn,/m/04lrdnt,/m/04lrdn5,/m/04lrdnh,/m/04lrdnb'

次の部分は、各値を配列に分割します。

lineArray = lineArray(1:lineIndex-1);              %# Remove empty cells, if needed
  for iLine = 1:lineIndex-1                        %# Loop over lines
    lineData = textscan(lineArray{iLine},'%s',...  %# Read strings
                        'Delimiter',',');
    lineData = lineData{1};                        %# Remove cell encapsulation
    if strcmp(lineArray{iLine}(end),',')           %# Account for when the line
      lineData{end+1} = '';                        %# ends with a delimiter
    end
    lineArray(iLine,1:numel(lineData)) = lineData; %# Overwrite line data
  end

これは以下を出力します:

'La Loi des rues'   '/m/0gw3lmk'    '/m/0gw1pvm'    []  []  []
'L''�tudiante'  '/m/0j9vjq5'    '/m/0h6hft_'    []  []  []
'The Kid From    Borneo'    '/m/04lrdnn'    '/m/04lrdnt'    '/m/04lrdn5'    '/m/04lrdnh'    '/m/04lrdnb'

問題は、UTF-8 エンコーディングが失われていることですtextscan(以前の配列では問題がなかったのに、疑問符が表示されることに注意してください)。

質問: {3x1} 配列を 3xN 配列に変換するときに、UTF-8 コーディングを維持するにはどうすればよいですか。

textscan既にワークスペースにある配列のに UTF-8 エンコーディングを保持する方法については何も見つかりません。すべては、問題のないテキスト ファイルのインポートに関係しています。これが 2 番目のステップです。

ありがとう!

4

2 に答える 2

1

次のコードを試してください。

%# read whole file as a UTF-8 string
fid = fopen('utf8.csv', 'rb');
b = fread(fid, '*uint8')';
str = native2unicode(b, 'UTF-8');
fclose(fid);

%# split into lines
lines = textscan(str, '%s', 'Delimiter','', 'Whitespace','\n');
lines = lines{1};

%# split each line into values
C = cell(numel(lines),6);
for i=1:numel(lines)
    vals = textscan(lines{i}, '%s', 'Delimiter',',');
    vals = vals{1};
    C(i,1:numel(vals)) = vals;
end

結果:

>> C
C = 
    'La Loi des rues'        '/m/0gw3lmk'    '/m/0gw1pvm'              []              []              []
    'L'Étudiante'            '/m/0j9vjq5'    '/m/0h6hft_'              []              []              []
    'The Kid From Borneo'    '/m/04lrdnn'    '/m/04lrdnt'    '/m/04lrdn5'    '/m/04lrdnh'    '/m/04lrdnb'

これをテストしたとき、入力CSVファイルを「BOMなしのUTF-8」としてエンコードしたことに注意してください(エディターとしてNotepad ++を使用していました)

于 2012-07-24T17:52:43.783 に答える
0

fopen現在のコマンドの代わりに、次のコマンドを使用してみてください。ファイルの UTF-8 エンコーディングを指定します。

f = fopen(filename,'rt', 'UTF-8');

おそらく、これを使用してコードの一部を短縮することもできます。

text = fscanf(f,'%c');
Lines = textscan(text,'%s','Delimiter',',');

これは、そこで行っている事前割り当ての一部を軽減するのに役立つ場合があります。

于 2012-07-24T17:48:35.203 に答える