Imanuel Jodi
Imanuel Jodi

Reputation: 37

Read csv file give weird result with encoding UTF8

So i have a problem while doing my project,i'm trying to read my csv file,i have try using encoding utf16 and utf8,and both give me weird result.

Below is the result from utf8

:�4^xr7zd5fixekg_2020-01-31T190000_88ffa7d6bd0e31b86d9c3ac3d7c601f5_c3575d.csv�[o�8���ST}��6Ɨ��U��msQ��j4!c�ԛ" U���k$�;���l�����ǿ��vp���;�a0.�Ggݳ���u�"�
��G��?
      �No��u^���%8>>v��ο9_�L��7燘�C���
��韊��m���9��Yn��Z���",2!':��T�dJ1�
                                    3N�d�Ea����l�鼼u�\�͙<L��H��F��ֲ(O�
                                                                      �Urj�$,Lu��ަ����:<ME�&u�B��DQuO'c1�3���<�v��өV�(�M�L�:��tl��悍�T_����~k%�.�5�D�����n^�J/���L$�X�b�=�T����id(S�W�%����>u#���m�y㯴-�s��z�M�2]�쩉uU��   ��|j��J��UC���Օ2=O��$����/�۰H':i.�p���k�Q����i����}81IIB/K!��S����<��������-�q�:�6ы������V:�_g�t����s��"��nN�MY��H9���sL?;�{:
                                                                             �󇘈�epΕV3�O
                                                                                        ����R[�g���I�����'}�/]B��b��y��P��G
                                                                                                                                ƜqB!#��TzHcJ�XBI�����b�b�A]��3�1��y]y8��'�� ��(�S;iw����f�B�3��9�y�!�c�J��G.�(v#D��
x��9����a�:�w�`|�&o]��#�hL%+ybc`y���B��f��3hK�'1�9G0��d
o�s����5�=�'����x��U�^_t��_�˳A�^�]u:�wH����Q;��G�����'��m�&-����U�ӗp0�c�{H��
                    ������l�;�^�~��j��!��t��!"���e�=�����eݼ�)k�r���Y�=��!;�~/Y�}�!�
                                                                                      ��6Ѷ�"��1��Y�}�C�֖�����-���ԮB^U|�I�ۈ�㹑����;E������6�,7��K�7�(�䒺�Ş���,\y,c)-��<�"��ǩ�e#@Ƒ��`o
                                                    ���3�q�[�ĝ(N����˿��t�K�|���t���"C��ë����f;�J�=Z�o_��i�����g��;��y4o_;�����U��%��!�� �`������dZ>!;��ԉw�/,

and Below is my code

function readFile(req, res, next) {
  const csvString = fs.readFileSync('./server/components/engagement/downloaded.csv', { encoding: 'utf8' })
  console.log(csvString.toString())
};

I can open the file with excel,but if i try to open it in visual studio code it says The file is not displayed in the editor because it is either binary or uses an unsupported text encoding

UPDATE

So i try to use package neat-csv https://flaviocopes.com/node-read-csv/ , it gives me weird string again,but i think i get closer to my goals this time(maybe ?). Below is the output from the package.

[ { '\u001f�\b\b:�4^\u0000\u0003xr7zd5fixekg_2020-01-31T190000_88ffa7d6bd0e31b86d9c3ac3d7c601f5_c3575d.csv\u0000�[o�8\u0014���ST}��6Ɨ��U��msQ��j4\u001a!c�ԛ\u0004" \u0019U���k\u0002$�;���l��\u0001���ǿ��vp���;�a0�\b.�Ggݳ���u�"�': '��G��?\f�No��u^\u0006���%8>>v��ο9_�L��7燘�C���' },
  { '\u001f�\b\b:�4^\u0000\u0003xr7zd5fixekg_2020-01-31T190000_88ffa7d6bd0e31b86d9c3ac3d7c601f5_c3575d.csv\u0000�[o�8\u0014���ST}��6Ɨ��U��msQ��j4\u001a!c�ԛ\u0004" \u0019U���k\u0002$�;���l��\u0001���ǿ��vp���;�a0�\b.�Ggݳ���u�"�':
     '��韊�\u0012�m���9��Yn��Z���",2!\':��T�dJ1�\u000b3N�\u0006d�Ea����l�鼼u\u0018�\\�͙<L��H��F��ֲ(O�\u000b�U\u0000rj�$,Lu��\u0011ަ���\u001d�:\u000f<ME�&u�B��DQuO\'c1�3�\u0014��\u000f<�v��\u0010өV�(\u001e�M�L�:��t\u001dl��悍�T_����~k%�.�5�D\u0017�����n^�J/���L$�X�b�=�T���\u001e�i\u0019d(S�W�%����>u#���m�y㯴-\u0019�s��z�\u0019M�2]�쩉uU��\t��|j��J��UC���Օ2=O��$����/�۰H\':i\u001a.�p���k�Q����i����}81IIB/K!��S����<\u000f��������\u001e-�q�:�6ы������V\u000e:�_g�\u0001\u000et�\u000f�\u0019��s��"�\u0000�nN\u0019�M\u0011Y��H9���sL?;�{:\u000b�󇘈�\u0010epΕV3�O\u000b�\u0007\u000f�\u0015��R[\u0003\u0002\u001b�g���I�����\'}\u0000�/]B��b�\u0011�y��P��G\fƜq\u0007B!\u0014#��TzHcJ\u0004�X�\bBI�����b�b�A]��\u00063�1��y]y8��\'�\u0001\u0002�\t��(�S;\u0010iw����f�B\u000e�3��\u001d\u00079�\u001dy�!�c��\bJ��G.�(v#D�\u0007\u0011�' },
  { '\u001f�\b\b:�4^\u0000\u0003xr7zd5fixekg_2020-01-31T190000_88ffa7d6bd0e31b86d9c3ac3d7c601f5_c3575d.csv\u0000�[o�8\u0014���ST}��6Ɨ��U��msQ��j4\u001a!c�ԛ\u0004" \u0019U���k\u0002$�;���l��\u0001���ǿ��vp���;�a0�\b.�Ggݳ���u�"�':
     '\u0000�\u0011�\bS\u0000\u0003�$�\\!�X�V\'A�\u001f�J�m�\u001f&x����;\u0010�\u0006~5\\8���n?<�\u0019�\u0006�w(\u0001��;\t<�HPR�d\rx��9����a\u001b�:�\u0006w�\u001c`\u001b\u0003|\to]��#�hL%+yb\u0017c`y���B��\u0000f��3hK�\'1�9�\bG0�' },
  { '\u001f�\b\b:�4^\u0000\u0003xr7zd5fixekg_2020-01-31T190000_88ffa7d6bd0e31b86d9c3ac3d7c601f5_c3575d.csv\u0000�[o�8\u0014���ST}��6Ɨ��U��msQ��j4\u001a!c�ԛ\u0004" \u0019U���k\u0002$�;���l��\u0001���ǿ��vp���;�a0�\b.�Ggݳ���u�"�':
     '2�bfg+�W�����U�-�E�5�U�^_t��_�˳A�\u0013^�]u:�wH���\u0013�Q;��G�����\'��m�&-����U�ӗp0\u001a�c�{H�\u0003�\ro�s���\u0003�5�=�\'����\u0011x�\u000b����\u000e�\u001b�l\u000f�\u0019;�^�~��j��!�\u001b7�t��!"���e�=�����eݼ\u0017�)k�r���Y�=��!;�~/Y�}�!�\u000b�\u0003�6Ѷ�"\u0010��1��Y�}�C\u0010�\u0007֖�����-\u0017���Ԯ\u0003\u0010B^U\u0004|�I�ۈ�㹑��\u0002��;E������6�\u0010,7��K�7\u001b�(�\u001e䒺�Ş���,`\b\\\u0004y,c\u001d)-��<\u0002�"\u0011��ǩ�e\u000f#@\u0000Ƒ��`o\u0005\f\u0018�\u0018\u0012�\u001e�3�q�[�ĝ(N��\u0014��˿\u0016��t�K\u001d\u001e�|���t���"C��ë����f;�J�=Z�o_\u0012��i�\u001f����g��;�\u001f�y4\u0017o_\u0018;\u0007�\u0012���\u001b�\u001fU\u0010��%��\u0005!�� �`����\u0001��d\u0002Z>!;\u001c��\u0005ԉw�/,\u0000\u0000' } ]

Upvotes: 0

Views: 2510

Answers (1)

darklightcode
darklightcode

Reputation: 2772

Your file looks like an xlsx, you should read the file using https://www.npmjs.com/package/exceljs package.

Check out the demo below:

const Excel = require('exceljs');
// read from a file
var filename = '/absolute/or/relative/path/to/file.xlsx';
var workbook = new Excel.Workbook();
workbook.xlsx.readFile(filename)
  .then(function() {
    // use workbook

    workbook.eachSheet(function(worksheet, sheetId) {

      worksheet.eachRow(function(row, rowNumber) {
        console.log('Row ' + rowNumber + ' = ' + JSON.stringify(row.values));

        // Iterate over all cells in a row (including empty cells)
        row.eachCell({
          includeEmpty: true
        }, function(cell, colNumber) {
          console.log('Cell ' + colNumber + ' = ' + cell.value);

          // update the current cell value
          worksheet.getCell(cell.fullAddress).value = cell.value + ' ' + cell.value;

        });

      });

    });

    // target a specific cell
    worksheet.getCell('A1').value = 'new value';

    // after everything is done you can write the data to another file
    workbook.xlsx.writeFile('/path/to/my_new_file.xlsx')
      .then(function() {
        // done
      });

  });

Upvotes: 1

Related Questions