首页 \ 问答 \ 文件编写时的Ruby编码(Ruby Encoding While File Writing)

文件编写时的Ruby编码(Ruby Encoding While File Writing)

我有一堆pdf / txt / msg文件我试图在ruby控制台中下载并移动到本地目录或重新上传到另一个目的地。

首先,我一直在尝试读取文件并将其写入本地目录,如下所示:

path = File.join 'temp', doc_name
file = File.new(path, 'w')
file << Document.find(123).fetch_file  // this function retrieves and decrypts the file from s3

我得到的例外是:Encoding :: UndefinedConversionError:“\ xB5”从ASCII-8BIT到UTF-8

我想知道如何在文件写入中获得正确的编码,以便我可以下载并打开它。 看起来它应该是微不足道的,答案可能是在解密或s3调用中,但这似乎与文件写入有关。


I have a bunch of pdf/txt/msg files I'm trying to download in the ruby console and either move to a local directory or re-upload to another destination.

To start, I've just been trying to read the file and write it to a local directory like so:

path = File.join 'temp', doc_name
file = File.new(path, 'w')
file << Document.find(123).fetch_file  // this function retrieves and decrypts the file from s3

The exception I get is: Encoding::UndefinedConversionError: "\xB5" from ASCII-8BIT to UTF-8

I'm wondering how I might be able to get the encoding correct on the file write so that I can download it and open it. It seems like it should be trivial, the answer could be in the decryption or s3 call, but this seemed related to the file write.


原文:https://stackoverflow.com/questions/48332596
更新时间:2022-04-23 12:04

最满意答案

memcached服务实际上并不为你安装PHP memcached扩展。 它只安装用于存储缓存的memcached服务器。

您需要首先从PECL存储库下载Windows DLL单击蓝色的Windows DLL链接 )。 然后,您必须将extension=php_memcache.dll行添加到SAPI的正确php.ini文件中。 另外,请注意扩展DLL文件需要放在XAMPP安装的正确路径中。

对于Apache,只需使用<?php phpinfo();在文档根目录下创建一个脚本<?php phpinfo(); 并尝试在您的网页浏览器中加载。 你应该在标有Loaded configuration(php.ini)的顶部看到一行,它给你加载php.ini文件的完整路径。 在Windows中,如果通过类似XAMPP的方式安装了PHP,路径实际上可能与phpinfo()所述的路径不同。 所以你可能需要依靠XAMPP来找到正确的php.ini文件。

对于CLI SAPI,您可以使用php.exe --ini来执行相同的操作。 同样,如果它修改了你的配置路径( 因为这是一个编译时指令 ),你可能需要依赖XAMPP包。

在对php.ini进行更改后,您需要重新启动PHP才能使更改生效。


由于您在Windows上使用PHP 7,因此可能很重要的一点是,从PECL编译的DLL实际上可能不适用于Windows的apache,因为您很可能使用了领导的SAPI。 所以确保你正在下载正确的版本。 据我所知,该版本只能编译到PHP 5.6以上。 在评论中提到的https://github.com/nono303/PHP7-memcahe-dll中提供的github替代方案,可在非线程安全下进行测试。 所以你可能只能在Windows上为你的CLI脚本工作。


The memcached service doesn't actually install the PHP memcached extension for you. It only installs the memcached server used to store your cache.

You'll need to download the Windows DLL from the PECL repository first (click on the blue Windows DLL link). Then you must add the extension=php_memcache.dll line to the correct php.ini file for your SAPI. Also, note the extension DLL file needs to be placed in the correct path for your XAMPP installation.

For Apache, simply create a script in your document root with the line <?php phpinfo(); and try loading that in your web browser. You should see a line at the top labeled Loaded configuration (php.ini) which gives you the full path to your loaded php.ini file. On Windows the path may actually appear different than what is stated in phpinfo() if you installed PHP through something like XAMPP. So you may need to rely on XAMPP to locate the correct php.ini file.

For the CLI SAPI, you can use php.exe --ini to do the same. Again, you may need to rely on the XAMPP package if it has modified your configuration path (since this is a compile time directive).

After making your changes to php.ini you will need to restart PHP for the changes to take effect.


Since you're using PHP 7 on Windows it's probably also important to note that the compiled DLL from PECL may not actually work under apache for Windows, because you're more than likely using a theaded SAPI. So make sure you are downloading the correct version. As far as I can tell that version is only compiled to work with up to PHP 5.6. The github alternative, for PHP 7, available at https://github.com/nono303/PHP7-memcahe-dll as mentioned in the comments is tested under non-thread safe. So you may only be able to get this working for your CLI scripts on Windows.

相关问答

更多
  • Andrei Zmievski(memcached插件的开发人员)根据以下说明友好地回答了我的电子邮件请求: $ pecl download memcached $ tar zxvf memcached-1.0.0.tgz (or whatever version downloads) $ cd memcached-1.0.0 $ phpize $ ./configure --with-libmemcached-dir=/opt/local $ make $ sudo make install 这工作完美 ...
  • 正如http://ca3.php.net/manual/en/intro.posix.php所述,POSIX扩展无法安装在Windows上。 注意:此扩展在Windows平台上不可用。 Windows外壳不支持颜色。 如果你想在Windows上使用Symfony MVC框架,你可能需要使用Cygwin或WAMP (或类似的东西)。 The POSIX extension cannot be installed on Windows as stated on http://ca3.php.net/manual ...
  • Memcached客户端库刚刚发布稳定。 它正在被digg (由Andrei Zmievski开发的digg开发,现在不再是digg),并且实现了比老的memcache客户端更多的memcached协议 。 memcached最重要的功能是: 卡通令牌 。 这使我的生活更容易,是一个简单的预防系统,用于陈旧的数据。 每当你从缓存中拉出一些东西,你可以收到一个cas令牌(一个双号码)。 您可以使用该令牌来保存更新的对象。 如果没有其他人更新您的线程运行时的值,交换将成功。 否则,将创建一个较新的cas令牌,并 ...
  • 在Windows中加载扩展时的错误并不像在Linux中那样有用,因此您需要像依赖者walker这样的工具来解决问题。 http://www.dependencywalker.com/ Errors while loading extensions in Windows are not as helpful as they are in Linux, so you need a tool like dependency walker to fish out the problem. http://www.de ...
  • 你需要: 将memcached库安装到您的操作系统中 安装php-client以使用memcache。 它可能是Memcache或Memcached。 如果你选择Memcached,你将需要安装必要的libevent版本 Memcache客户端更稳定,但Memcached有一些有趣的功能。 看看功能上的差异(只有很少的功能),如果你真的需要这些新功能 - 取Memcached,否则 - Memcache。 You need to: install memcached library into your OS ...
  • memcached服务实际上并不为你安装PHP memcached扩展。 它只安装用于存储缓存的memcached服务器。 您需要首先从PECL存储库下载Windows DLL ( 单击蓝色的Windows DLL链接 )。 然后,您必须将extension=php_memcache.dll行添加到SAPI的正确php.ini文件中。 另外,请注意扩展DLL文件需要放在XAMPP安装的正确路径中。 对于Apache,只需使用
  • 你使用了错误的语法。 第三个参数是压缩标志。 制作一个简单的界面,如下所示。 它可以帮助你: /* defines params */ define('MEMCACHED', 1); define('CACHE_DEFAULT_EXPIRE', 3600); if(MEMCACHED) if(! class_exists('memcached')) die('memcache not loaded'); /* Cache */ if(MEMCACHED) { global $memc ...
  • 问题是编译器版本不匹配(我有vc6 for php和memcache是从vc9构建的)。 这个讨论中的帖子之一表示php已经放弃了对vc6的支持,所以我重新安装了xmpp,其中包含带有vc9的php 5.4。 在http://windows.php.net/downloads/pecl/releases/memcache/3.0.6/php_memcache-3.0.6-5.4-ts-vc9-x86.zip中使用了memcache dll,并且每个人都运行良好! 谢谢您的帮助。 The issue was ...
  • MemcacheD是一个守护进程,它似乎已经将它作为服务安装。 现在您需要设置Memcahce PHP扩展,因此它将作为MemcacheD守护进程的客户端工作。 您需要搜索php_memcache.dll(最后没有D字母)。 你可以在这里找到它: http : //windows.php.net/downloads/pecl/releases/memcache/3.0.8/ MemcacheD is a daemon and it seems like you have installed it as a ...
  • 经过一番研究,我想到了如何使用标准的Memcache命令来完成它。 但是,不幸的是,这将导致人们前往memcache服务器,因此需谨慎使用; 在我的情况下,它仅在部署后使用: /** * Helper function to expire memcache entries with a specific key prefix. * * @param string $key_prefix * The memcache key prefix. * @param array $servers * ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)