首页 \ 问答 \ ruby中的csv文件编码问题(csv file encoding issue in ruby)

ruby中的csv文件编码问题(csv file encoding issue in ruby)

我使用ruby解析csv文件并收到错误

invalid byte sequence in utf-8 csv

我试过编码选项

    CSV.foreach(path, {headers: true, encoding: 'windows-1251:utf-8'}) do |row|
        new_row = {}
        headers = []
        row.each do |k,v|
            headers << k
            v = v.force_encoding('UTF-8') || ''
            v.gsub! "\xE2\x80\x96", "-"
            v.gsub! "\xE2\x80\x93", "-"
            v.gsub! "\xE2\x80\x94", "-"
            v.gsub! "\xE2\x80\x95", "-"
            v.gsub! "\xE2\x80\x98", "'"
            v.gsub! "\xE2\x80\x99", "'"
            v.gsub! "\xE2\x80\x9C", "\""
            v.gsub! "\xE2\x80\x9D", "\""
            v.gsub! "\xE2\x80\xA6", "..."
            v.gsub! "\x0D\x0A", "\n"
            v.gsub! "\xC2\xA0", " "
            v.gsub! "\xC2\xB0", " "

            new_row[k] = v
        end

        output_csv.puts headers if output_csv.header_row?
        output_csv.puts new_row
    end

现在我结束了

incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

在CSV文件中引发此问题的字符串是“G ran”

下面是示例输入行

David Evans & Assocs    www.deainc.com  13858534    jpv@deainc.com  G�ran   Volk    5034990383

任何人都可以建议我如何解决这个问题。


I parsing a csv file using ruby and getting an error

invalid byte sequence in utf-8 csv

I tried with encoding option

    CSV.foreach(path, {headers: true, encoding: 'windows-1251:utf-8'}) do |row|
        new_row = {}
        headers = []
        row.each do |k,v|
            headers << k
            v = v.force_encoding('UTF-8') || ''
            v.gsub! "\xE2\x80\x96", "-"
            v.gsub! "\xE2\x80\x93", "-"
            v.gsub! "\xE2\x80\x94", "-"
            v.gsub! "\xE2\x80\x95", "-"
            v.gsub! "\xE2\x80\x98", "'"
            v.gsub! "\xE2\x80\x99", "'"
            v.gsub! "\xE2\x80\x9C", "\""
            v.gsub! "\xE2\x80\x9D", "\""
            v.gsub! "\xE2\x80\xA6", "..."
            v.gsub! "\x0D\x0A", "\n"
            v.gsub! "\xC2\xA0", " "
            v.gsub! "\xC2\xB0", " "

            new_row[k] = v
        end

        output_csv.puts headers if output_csv.header_row?
        output_csv.puts new_row
    end

now i'm ended up with

incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)

The string which is raising this issue in CSV file is "G�ran"

Below is the sample input row

David Evans & Assocs    www.deainc.com  13858534    jpv@deainc.com  G�ran   Volk    5034990383

Can anyone suggest me how to solve this issue.


原文:https://stackoverflow.com/questions/28495462
更新时间:2021-12-06 19:12

最满意答案

根据旧的Oracle 代码示例

OracleResultSetMetaData接口不实现getSchemaName()和getTableName()方法,因为底层协议不能使其可行。

这意味着,至少在使用Oracle驱动程序时, ResultSetMetaData也不会为Oracle提供这些方法。 (我尝试使用OCI驱动程序,看看是否有所作为,但显然没有)。

有一个WebLogic 8 文档表明它可以完成,但该类型4驱动程序在以后的版本中已被弃用。 因此,您可能仍然可以找到支持针对Oracle的getSchemaName()的第三方驱动程序,但似乎不太可能。


According to an old Oracle code sample:

OracleResultSetMetaData interface does not implement the getSchemaName() and getTableName() methods because underlying protocol does not make this feasible.

That implies to me that ResultSetMetaData will not have those methods for an Oracle either, at least when using an Oracle driver. (I tried with the OCI driver to see if that made a difference, but apparently not).

There's a WebLogic 8 document that suggests it could be done, but that type 4 driver has been deprecated in later releases. So it's possible you may still be able to find a third-party driver that supports getSchemaName() against Oracle, but it seems unlikely.

相关问答

更多
  • 不,它不会那样工作。 JDBC只是本地数据库的一个包装。 但是JDBC或数据库游标工作原理都是一样的: 你发送一个查询(通过JDBC)到数据库 数据库分析查询并初始化游标(JDBC中的ResulSet) 当您从ResultSet中获取数据时,数据库软件将在数据库中走动以获取新行并填充ResultSet 所以对ResultSet可以包含的行数没有限制,对java客户端程序可以处理的行数也没有限制。 如果您尝试加载内存中的所有行来填充列表以及耗尽客户端应用程序内存 ,则唯一的限制就来了。 但是,如果您处理行并且 ...
  • 好的,我自己想到了: 如果您转到数据源 - > 配置 - > 连接池 - >单击高级 , 在那里有Init SQL将会被执行用于初始化新创建的物理数据库连接,我们可以把: SQL ALTER SESSION SET CURRENT_SCHEMA=animals 制作默认模式。 Ok, I figured it out myself: If you go to Data Source-> Configuration -> Connection Pool -> click Advanced, There is ...
  • Select owner from dba_tables where table_name = 'YOUR_TABLE_NAME' 在这里你可以找到更多。 Select owner from dba_tables where table_name = 'YOUR_TABLE_NAME' Here you can find something more.
  • 您可以从ResultSetMetaData获取列编号: Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(query); ResultSetMetaData rsmd = rs.getMetaData(); int columnsNumber = rsmd.getColumnCount(); You can get columns number from ResultSetMetaData: Statement s ...
  • 这是因为你在循环之前调用rs.next() 。 它返回一个布尔值,由于这个语句,一行失去了打印的机会。 Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@host:1521:DB", "User","pass_01"); Statement statement = connection.createStatement(); resultset = statement.executeQuery("select * ...
  • 根据旧的Oracle 代码示例 : OracleResultSetMetaData接口不实现getSchemaName()和getTableName()方法,因为底层协议不能使其可行。 这意味着,至少在使用Oracle驱动程序时, ResultSetMetaData也不会为Oracle提供这些方法。 (我尝试使用OCI驱动程序,看看是否有所作为,但显然没有)。 有一个WebLogic 8 文档表明它可以完成,但该类型4驱动程序在以后的版本中已被弃用。 因此,您可能仍然可以找到支持针对Oracle的getSc ...
  • 假设您使用spring数据源定义数据库连接,则可以在定义数据源配置时设置默认模式: spring.datasource.schema = #value for your default schema to use in database 你可以在这里找到更多的信息: Spring Boot Reference Guide。 附录A.通用应用程序属性 在做了一些研究之后,看起来像Oracle驱动程序不会让您设置一个默认模式来处理,如下所述: Oracle连接URL中的默认架构 从那篇文章中,你有两个选择: ...
  • 这似乎是这条线抛出异常: ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("KnownLanguages", connection); 我认为问题在于它没有解决数据库中的类型名称。 我相信我在过去发现,类型查找是区分大小写的,默认情况下在Oracle中,类型名称全是大写。 因此,请尝试使用上述行中的“KNOWNLANGUAGES”。 此外,它似乎在寻找在SYSTEM模式,虽然我不知道为什么。 如果类型不属于SYSTEM, ...
  • 要让它工作,您应该指定stored-procedure-name和模式名称: stored-procedure-name="XX_EMPROC.GET_FRMA" 但同时你必须指明: ignore-column-meta-data="true" 这意味着:
  • 好。 经过尝试并且过去3天失败后,我找到了一个部分,我不需要在任何地方放置任何CLASSPATH等。 我不知道解决方案是否正确,但它确实有效。 只需将ojdbc6.jre复制到 Linux的 /usr/lib/jvm/YOUR_ORACLE/jre/lib/ext/ 在Windows上 C:/program files/java/jre_yourversion/lib/ext/ Ok. After Trying and failing for past 3 days, i came up to a pa ...

相关文章

更多

最新问答

更多
  • 您如何使用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)