Illegal quoting in line 1 using Ruby CSV

前端 未结 2 716
春和景丽
春和景丽 2021-01-25 07:22

I am getting this error:

Illegal quoting in line 1. (CSV::MalformedCSVError)

Line 1 in my file is as follows:

\"Status\"    \"Intern         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-25 07:56

    On *nix systems the file command is used to give a reasonable-hint to what the file contents are:

    file /usr/share/dict/words
    /usr/share/dict/words: ASCII text
    
    file /usr/bin/ruby
    /usr/bin/ruby: Mach-O universal binary with 2 architectures
    /usr/bin/ruby (for architecture i386):  Mach-O executable i386
    /usr/bin/ruby (for architecture x86_64):  Mach-O 64-bit executable x86_64
    

    If you're on *nix, try running that against your CSV file and see what it says. It's not fool-proof, but it's reasonably accurate.

    As something to get you started, here's how to convert space-delimited fields to tab-delimited:

    row = '"Status"    "Internal ID"   "Language"  "Created At"    "Updated At"    "IP Address"    "Location"  "Username"  "GET Variables" "Referrer"  "Number of Saves"   "Weighted Score"    "Completion Time"   "Invite Code"   "Invite Email"  "Invite Name"   "Invite: branchid"  "Invite: lastname"  "Invite: clientname"    "Invite: membershipid"  "Invite: clientid"  "Invite: dateofbirth"   "Invite: membershiptype"    "Invite: branch"    "Invite: unitid"    "Invite: shortname" "Invite: changedatetime"    "Invite: homephone" "Collector" '
    row.gsub!(/"\s+"/, %Q["\t"]) # => "\"Status\"\t\"Internal ID\"\t\"Language\"\t\"Created At\"\t\"Updated At\"\t\"IP Address\"\t\"Location\"\t\"Username\"\t\"GET Variables\"\t\"Referrer\"\t\"Number of Saves\"\t\"Weighted Score\"\t\"Completion Time\"\t\"Invite Code\"\t\"Invite Email\"\t\"Invite Name\"\t\"Invite: branchid\"\t\"Invite: lastname\"\t\"Invite: clientname\"\t\"Invite: membershipid\"\t\"Invite: clientid\"\t\"Invite: dateofbirth\"\t\"Invite: membershiptype\"\t\"Invite: branch\"\t\"I...
    

提交回复
热议问题