帖子已更新.如果您已阅读已发布的问题,请转到解决方案部分.谢谢!
这是展示我的问题的最小化代码:
用于测试的输入数据文件已由Window的内置记事本保存为UTF-8编码.它有以下三行:
abacus æb?k?s abalone æb?l?uni abandon ?bænd?n
Perl脚本文件也被Window的内置记事本保存为UTF-8编码.它包含以下代码:
#!perl -w
use Data::Dumper;
use strict;
use autodie;
open my $in,'<',"./hash_test.txt";
open my $out,'>',"./hash_result.txt";
my %hash = map {split/\t/,$_,2} <$in>;
print $out Dumper(\%hash),"\n";
print $out "$hash{abacus}";
print $out "$hash{abalone}";
print $out "$hash{abandon}";
Run Code Online (Sandbox Code Playgroud)
在输出中,哈希表似乎没问题:
$VAR1 = {
'abalone' => 'æb?l?uni
',
'abandon' => '?bænd?n',
'?abacus' => 'æb?k?s
'
};
但实际上并非如此,因为我只获得两个值而不是三个:
æb?l?uni ?bænd?n
Perl给出以下警告消息:
Use of uninitialized value $hash{"abacus"} in string at C:\test2.pl line 11, <$i
n> line 3.
哪里有问题?有人可以解释一下吗?谢谢.
解决方案
数百万的感谢你们所有人:)现在终于找到了罪魁祸首,问题变得可以解决了:)正如@Sinan深刻地指出的那样,我现在100%确定导致上述问题的罪魁祸首是两个BOM的字节,当记事本保存为UTF-8时,记事本添加到我的数据文件中,并且Perl不知道如何正确处理.虽然很多人建议我应该使用"<:utf8"和">:utf8"来读写文件,但事情是这些utf-8配置并没有解决问题.相反,它们可能会导致其他一些问题 …
为什么保存为UTF8的文件(在Notepad ++中)在我在c ++程序中打开它的fstream开头有这个字符?
'╗┐
我不知道它是什么,我只知道当我保存到ASCII时它不存在.更新:如果我将它保存为UTF8(没有BOM),它就不存在了.
如何在c ++中检查文件的编码(ASCII或UTF8,其他所有内容将被拒绝;)).这正是这些人物吗?
谢谢!
我正在使用ICU的ustdio函数将UnicodeString对象写入一系列编码中的文件,但它似乎不会添加BOM.
我的代码:
void write_file(const char* filename, UnicodeString &str) {
UFILE* f = u_fopen(filename, "w", NULL, "UTF-16 LE");
u_file_write(str.getTerminatedBuffer(), str.length() + 1, f);
u_fclose(f);
}
int _tmain(int argc, _TCHAR* argv[])
{
UnicodeString str(L"?????????");
write_file("test.txt", str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我将LE更改为BE时,文件编码会进行交换,但是没有BOM,十六进制编辑器中的输出文件是:
A0 03 B1 03 C1 03 B8 03 AD 03 BD 03 C9 03 BD 03 97 03 00 00
Run Code Online (Sandbox Code Playgroud)
注意:如果我将代码页设置为"UTF-16",则会有一个BOM,但是一旦我手动指定了字节序,它就会消失.
或者有没有办法将UnicodeString写入带有BOM的文件?
我试图将文件合并到一个输出文件中,我的个人文件上有BOM,我怎么能在使用Stream.CopyTo方法时摆脱它.
我的代码看起来像这样:
using (var output = File.Open(outputFile,FileMode.Append,FileAccess.Write))
{
foreach (var inputFile in inputFiles)
{
using (var input = File.OpenRead(inputFile))
{
input.CopyTo(output);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在将常规文件读入我已编写的程序时遇到一些麻烦.我目前遇到的问题是pdf基于某种突变的utf-8,其中包括一个BOM,它会在我的整个操作中引发一个问题.在我的应用程序中,我正在使用需要ascii输入的Snowball词干算法.有许多主题涉及到为utf-8解决错误,但是没有一个涉及将它们发送到Snowball算法,或者考虑ascii是我想要的最终结果.目前我使用的文件是使用标准ANSI编码的记事本文件.我得到的具体错误信息是这样的:
File "C:\Users\svictoroff\Desktop\Alleyoop\Python_Scripts\Keywords.py", line 38, in Map_Sentence_To_Keywords
Word = Word.encode('ascii', 'ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
我的理解是在python中,包括ignore参数只会传递遇到的任何非ascii字符,这样我就会绕过任何BOM或特殊字符,但显然不是这种情况.调用的实际代码在这里:
def Map_Sentence_To_Keywords(Sentence, Keywords):
'''Takes in a sentence and a list of Keywords, returns a tuple where the
first element is the sentence, and the second element is a set of
all keywords appearing in the sentence. Uses Snowball algorithm'''
Equivalence = stem.SnowballStemmer('english')
Found = []
Sentence = re.sub(r'^(\W*?)(.*)(\n?)$', r'\2', Sentence)
Words = Sentence.split()
for …Run Code Online (Sandbox Code Playgroud) 我有问题用utf-8编码写入mysql DB.我的应用程序有点复杂,所以我会尝试尽可能具体.(我的申请要求斯洛伐克特殊字体(有utf-8),如ľščťžýáí等.
我正在运行debian.我相信我的语言环境设置正确,但要确定:
root@radiator:/var/scripts# locale
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory
LANG=sk_SK.UTF-8
LANGUAGE=sk_SK.UTF-8:cs_CZ.UTF-8
LC_CTYPE="sk_SK.UTF-8"
LC_NUMERIC="sk_SK.UTF-8"
LC_TIME="sk_SK.UTF-8"
LC_COLLATE="sk_SK.UTF-8"
LC_MONETARY="sk_SK.UTF-8"
LC_MESSAGES="sk_SK.UTF-8"
LC_PAPER="sk_SK.UTF-8"
LC_NAME="sk_SK.UTF-8"
LC_ADDRESS="sk_SK.UTF-8"
LC_TELEPHONE="sk_SK.UTF-8"
LC_MEASUREMENT="sk_SK.UTF-8"
LC_IDENTIFICATION="sk_SK.UTF-8"
LC_ALL=
Run Code Online (Sandbox Code Playgroud)
我有bash脚本应该写文本(用斯洛伐克语写到DB.)(第一个哈希字符是因为debian不知道使用BOM,还是不知道如何处理它)
#
#!/bin/bash
table=$1
cycle=$2
sstart=$3
eend=$4
dbtext=$(cat /var/www/vids/$5/vars/$5.recogn.p.tmp2)
qry="INSERT INTO \`video\`.\`$table\` (\`DB_ID\` , \`LNX_ID\` , \`STIME\` , \`ETIME\` , \`TEXT\` ) VALUES ( …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码解压缩并保存CSV文件:
with gzip.open(filename_gz) as f:
file = open(filename, "w");
output = csv.writer(file, delimiter = ',')
output.writerows(csv.reader(f, dialect='excel', delimiter = ';'))
Run Code Online (Sandbox Code Playgroud)
一切似乎都有效,除了文件中的第一个字符是意外的.谷歌搜索似乎表明它是由于文件中的BOM.
我已经读过在utf-8-sig中编码内容应该可以解决这个问题.但是,添加:
.read().encoding('utf-8-sig')
Run Code Online (Sandbox Code Playgroud)
到csv.reader中的f失败:
File "ckan_gz_datastore.py", line 16, in <module>
output.writerows(csv.reader(f.read().encode('utf-8-sig'), dialect='excel', delimiter = ';'))
File "/usr/lib/python2.7/encodings/utf_8_sig.py", line 15, in encode
return (codecs.BOM_UTF8 + codecs.utf_8_encode(input, errors)[0], len(input))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)
如何删除BOM并将内容保存为正确的utf-8?
当我使用函数readTheNRow with row = 0(我读第一行)时,我发现三个第一个字符是\ 357,\ 273和\ 277.我发现这个前缀是与UTF-8文件有关的一些,但有些文件有这个前缀,有些文件没有:(.我如何忽略我想从中读取的文件中所有类型的此类前缀?
int readTheNRow(char buff[], int row) {
int file = open("my_file.txt", O_RDONLY);
if (file < 0) {
write(2, "closing fifo was unsuccessful\n", 31);
exit(-1);
}
// function's variables
int i = 0;
char ch; // a temp variable to read with it
int check; // helping variable for checking the read function
// read till we reach the needed row
while (i != row) {
// read one char
check = read(file, &ch, …Run Code Online (Sandbox Code Playgroud) 当我尝试使用节点js运行echo.js文件时,我从终端(powershell)收到以下错误消息
PS C:\Users\ASUS\Dropbox\Web Development\Backend\Intro To Node> node echo.js
C:\Users\ASUS\Dropbox\Web Development\Backend\Intro To Node\echo.js:1
(function (exports, require, module, __filename, __dirname) { ??
^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Run Code Online (Sandbox Code Playgroud)