【源码】读取任何(文本)文件UTF8或ANSI

【源码】读取任何(文本)文件UTF8或ANSI_第1张图片

如果你想用Matlab读取一个(文本)文件,你需要知道它是什么编码:UTF-8(“现代”标准)或ANSI(US-ASCII,旧的标准)。

If you want to read a (text) file with Matlab, you need to know what encoding it is: UTF-8 (the ‘modern’ standard) or ANSI (US-ASCII, the ‘old’ standard).

如果你想自动读取文件,你不一定知道你必须猜测编码形式。

If you have files you want to read automatically, where you don’t necessarily know the encoding you would have to guess.

有时会导致奇怪的文本,您在3或4个步骤后才会注意到。

This will sometimes result in strange text that you only notice 3 or 4 steps later.

本代码通过提供一种读取文件的单一方式来解决这个问题。

This submission takes care of that problem by providing a single way of reading a file.

保留前导和尾随空格,保留空行,并且可以处理UTF-8文件和ANSI文件。

It preserves leading and trailing spaces, it preserves empty lines, and it can handle both UTF-8 files and ANSI files.

请注意,虽然编码应该以特殊的前导位指定,但是用Matlab无法读取。

Note that although the encoding should be specified in a special leading bit, it is not possible to read this with Matlab.

因此,有可能使用错误的编码读取文件,尽管这种情况非常罕见。

It is therefore possible that a file is read with the wrong encoding, although this should be a very rare occurrence.

也可以将文件名作为URL输入。

It is also possible to enter the file name as a URL.

在这种情况下,此函数将把文件下载到临时目录中,读取后删除它。

In that case this function will download the file to the temporary directory, read it, and delete it.

Licence: CC by-nc-sa 4.0

更多精彩文章请关注公众号:【源码】读取任何(文本)文件UTF8或ANSI_第2张图片

你可能感兴趣的:(【源码】读取任何(文本)文件UTF8或ANSI)