我知道UTF文件有用于确定编码的BOM但是其他编码却不知道如何猜测编码.
我是新的java程序员.我编写了使用UTF BOM猜测UTF编码的代码.但我有其他编码的问题.我怎么猜他们.
有人可以帮帮我吗?提前致谢.
我最近在我的rails应用程序上进行了一系列测试,其中包含各种输入,我发现了传入请求中的空字符如何处理的问题.我的应用程序由Postgresql 8.4数据库支持.事实证明,Postgresql不支持在标准"text"或"varchar"字段中存储空字符.但是,当我尝试通过rails存储其中包含空字符的字符串时,默认行为似乎是在空字符后截断字符串.我认为这是一个问题,因为在截断发生之前验证了字符串.可以通过插入空字符来绕过诸如长度之类的事物的验证,因为ruby可以完美地处理它们并且截断仅发生在插入上.
我正试图找出处理这些输入的最佳方法.对我来说理想的行为就是在处理无效的UTF-8字节时抛出异常.现在,我能想到的唯一选择是显式检查每个输入字符串是否为空字符.我宁愿采用一种通用方法,但我不确定在哪里开始寻找.修补postgresql适配器来检查这是一个选项吗?还是有一些我一直缺少的标准方法?
我想在评论中匹配所有提到的用户.例:
var comment = '@Agneš, @Petar, please take a look at this';
var mentionedUsers = comment.match(/@\w+/g);
console.log(mentionedUsers)Run Code Online (Sandbox Code Playgroud)
我期待["@Agneš", "@Petar"]但是得到["@Agne", "@Petar"].如您所见,š符号不匹配.
如何匹配所有字母符号包括非ascii?
所以你知道你可以使用AsynchronousFileChannel将整个文件读取到String:
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(filePath, StandardOpenOption.READ);
long len = fileChannel.size();
ReadAttachment readAttachment = new ReadAttachment();
readAttachment.byteBuffer = ByteBuffer.allocate((int) len);
readAttachment.asynchronousChannel = fileChannel;
CompletionHandler<Integer, ReadAttachment> completionHandler = new CompletionHandler<Integer, ReadAttachment>() {
@Override
public void completed(Integer result, ReadAttachment attachment) {
String content = new String(attachment.byteBuffer.array());
try {
attachment.asynchronousChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
completeCallback.accept(content);
}
@Override
public void failed(Throwable exc, ReadAttachment attachment) {
exc.printStackTrace();
exceptionError(errorCallback, completeCallback, String.format("error while reading file [%s]: %s", path, exc.getMessage()));
}
};
fileChannel.read(
readAttachment.byteBuffer,
0,
readAttachment, …Run Code Online (Sandbox Code Playgroud) 这就是我正在做的事情,我在网站上进行网络爬虫以供个人使用,以复制文本并将书籍的章节设置为文本格式,然后使用另一个程序将其自动转换为 pdf 以将其放入我的云中。一切都很好,直到发生这种情况:特殊字符无法正确复制,例如重音在文本文件上显示为:\xe2\x80\x99,而 - 显示为\xe2\x80\x93。我用过这个(Python 3):
for text in soup.find_all('p'):
texta = text.text
f.write(str(str(texta).encode("utf-8")))
f.write('\n')
Run Code Online (Sandbox Code Playgroud)
因为我在读取这些字符时遇到了错误并且它刚刚停止了我的程序,所以我将所有内容编码为 utf-8 并使用 python 的方法 str() 将所有内容重新转换为字符串
如果有人对我的问题有更好的解决方案,我将发布整个代码,这是从第 1 页爬行网站到 max_pages 的部分,您可以在第 21 行修改它以获取本书的更多或更少章节:
import requests
from bs4 import BeautifulSoup
def crawl_ATG(max_pages):
page = 1
while page <= max_pages:
x= page
url = 'http://www.wuxiaworld.com/atg-index/atg-chapter-' + str(x) + "/"
source = requests.get(url)
chapter = source.content
soup = BeautifulSoup(chapter.decode('utf-8', 'ignore'), 'html.parser')
f = open('atg_chapter' + str(x) + '.txt', 'w+')
for text in soup.find_all('p'):
texta = text.text
f.write(str(str(texta).encode("utf-8"))) …Run Code Online (Sandbox Code Playgroud) I tried to persist UTF-8 as the default encoding in Python.
I tried:
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
Run Code Online (Sandbox Code Playgroud)
And I also tried:
>>> import sys
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('UTF8')
>>> sys.getdefaultencoding()
'UTF8'
>>>
Run Code Online (Sandbox Code Playgroud)
But after closing the session and opening a new session, the following was the result:
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
Run Code Online (Sandbox Code Playgroud)
How can I persist my changes? (I know that it's not always a good idea to change to UTF-8. It's in a …
我想从对日语文本进行 OCR 识别后收到的字符串中转换数字。
例如,当我提取日期时:
?? ? ? ? ?? ?
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
31 ? 12 ? 13 ?
Run Code Online (Sandbox Code Playgroud)
实现它的最佳方法是什么?
尝试使用powershell脚本中的iconv将某些文件的编码从ISO-8859-1批量转换为UTF-8时出现问题.
我有这个bat文件,工作正常:
for %%f in (*.txt) do (
echo %%f
C:\"Program Files"\GnuWin32\bin\iconv.exe -f iso-8859-1 -t utf-8 %%f > %%f.UTF_8_MSDOS
)
Run Code Online (Sandbox Code Playgroud)
我需要转换目录结构上的所有文件,所以我编写了这个其他脚本,这次使用powershell:
Get-ChildItem -Recurse -Include *.java |
ForEach-Object {
$inFileName = $_.DirectoryName + '\' + $_.name
$outFileName = $inFileName + "_UTF_8"
Write-Host Convirtiendo $inFileName -> $outFileName
C:\"Program Files"\GnuWin32\bin\iconv.exe -f iso-8859-1 -t utf-8 $inFileName > $outFileName
}
Run Code Online (Sandbox Code Playgroud)
使用此结果是将文件转换为UTF-16.我不知道我做错了什么.
任何人都可以帮我这个吗?对于PowerShell本身的编码可能是某种问题吗?
我正在使用W7和WXP以及LibIconv 1.9.2
我在Windows 7上运行Active Perl 5.14.我正在尝试编写一个程序,它将读入转换表,然后处理文件并用其他模式替换某些模式 - 以上所有Unicode(UTF-8) .这是该计划的开始:
#!/usr/local/bin/perl
# Load a conversion table from CONVTABLE to %ConvTable.
# Then find matches in a file and convert them.
use strict;
use warnings;
use Encode;
use 5.014;
use utf8;
use autodie;
use warnings qw< FATAL utf8 >;
use open qw< :std :utf8 >;
use charnames qw< :full >;
use feature qw< unicode_strings >;
my ($i,$j,$InputFile, $OutputFile,$word,$from,$to,$linetoprint);
my (@line, @lineout);
my %ConvTable; # Conversion hash
print 'Conversion table: opening file: E:\My Documents\Perl\Conversion table.txt'."\n"; …Run Code Online (Sandbox Code Playgroud)