2017-02-08 11 views
2

二重引用符とカンマの両方を含む長いテキストフィールドを含む壊れたCSVファイルがあります。私はある程度それをきれいにすることができましたし、タブ区切りのフィールドを行全体のベクトルとして持っています(それぞれの値は行です)。ベクトルの不正な二重引用符をRに置き換えます

head(temp, 2) 
[1] "\"org_order\"\t\"organizations.api_path\"\t\"permalink\"\t\"api_path\"\t\"web_path\"\t\"name\"\t\"also_known_as\"\t\"short_description\"\t\"description\"\t\"profile_image_url\"\t\"primary_role\"\t\"role_company\"\t\"role_investor\"\t\"role_group\"\t\"role_school\"\t\"founded_on\"\t\"founded_on_trust_code\"\t\"is_closed\"\t\"closed_on\"\t\"closed_on_trust_code\"\t\"num_employees_min\"\t\"num_employees_max\"\t\"stock_exchange\"\t\"stock_symbol\"\t\"total_funding_usd\"\t\"number_of_investments\"\t\"homepage_url\"\t\"created_at\"\t\"updated_at\""                                                                                                                
[2] "1\t\"organizations/care1st-health-plan-arizona\"\t\"care1st-health-plan-arizona\"\t\"organizations/care1st-health-plan-arizona\"\t\"organization/care1st-health-plan-arizona\"\t\"Care1st Health Plan Arizona\"\t\"\"\t\"Care1st Health Plan Arizona provides high quality health care services.\"\t\"Care1st is a health plan providing support and services to meet the health care needs of eligible members enrolled in KidsCare, AHCCCS, and DDD.\"\t\"http://public.crunchbase.com/t_api_images/v1475743278/m2teurxnhkwacygzdn2m.png\"\t\"company\"\t\"\"\t\"\"\t\"\"\t\"\"\t\"2003-01-01\"\t\"4\"\t\"FALSE\"\t\"\"\t\"0\"\t\"251\"\t\"500\"\t\"\"\t\"\"\t\"0\"\t\"0\"\t\"\"\t\"1475743348\"\t\"1475899305\"" 

次にtempをファイルとして書き出して読み込みます(これはtextConnectionよりもはるかに高速です)。ライン66951ませんでした:9月= 9月、引用=引用符、12月 = 12月には、何を、=何、スキャン(ファイル=ファイル内

エラー:しかし、read.table("temp", sep = "\t", quote = "\"", encoding = "UTF-8", colClasses = "character")は、特定の行にチョークと私のようなメッセージを与えます29個の要素

を持っている私は、これが原因次の行(不正な引用符が直後に見つけることができる「トリップ・デ・ラ・サント?」)のような不正な二重引用符にあると思います。

temp[66951] 
[1] "67654\t\"organizations/docotop\"\t\"docotop\"\t\"organizations/docotop\"\t\"organization/docotop\"\t\"DOCOTOP\"\t\"\"\t\"Le 'TripAdvisor de la sant?\" est arriv?. Docotop permet de trouver le meilleur professionnel de sant?gr?e ?la communaut?de patients\"\t\"\"\t\"http://public.crunchbase.com/t_api_images/v1455271104/ry9lhcfezcmemoifp92h.png\"\t\"company\"\t\"TRUE\"\t\"\"\t\"\"\t\"\"\t\"2015-11-17\"\t\"7\"\t\"\"\t\"\"\t\"0\"\t\"1\"\t\"10\"\t\"EURONEXT\"\t\"\"\t\"0\"\t\"0\"\t\"http://docotop.com/\"\t\"1455271299\"\t\"1473443321\"" 

私が提案しています不正な二重引用符を置き換える一重引用符で囲まれていますが、私は期待される引用符を残す必要があります。引用符はセパレータ(タブ)の直前または直後、最初(最初の行のみ)および行の終わりに期待されます。私は、タブとラインの開始と終了のための前後参照して正規表現で次の試みを書いたが、それは動作しません:

temp <- gsub("(?<![^\t])\"(?![\t$])", "'", temp, perl = T) 

編集:私はakrunのソリューション@しようとしたが、取得:

をライン181は引き起こさなかった(問題の29個の要素

ラインを持っていなかった:何、9月は= 9月、引用=引用符、12月 = 12月、=何スキャン(ファイル=ファイル、でエラーが発生しましたエラー前):

temp[181] 
[1] "198\torganizations/playfusion\tplayfusion\torganizations/playfusion\torganization/playfusion\tPlayFusion\t\tPlayFusion is a developer of computer games.\tPlayFusion is pioneering the next generation of connected interactive entertainment. PlayFusion's proprietary technology platform fuses video games, robotics, toys, and trans-media entertainment. The company is currently working on its own original IP to trail-blaze its vision ahead of opening its platform to others. PlayFusion is an independent, employee-owned company with offices in Cambridge and Derby in the UK, Douglas in the Isle of Man, and New York and San Francisco in the USA.\thttp://public.crunchbase.com/t_api_images/v1475688372/xnhrd4t254pxj6yxegzt.png\tcompany\t\t\t\t\t2015-01-01\t4\tFALSE\t\t0\t11\t50\t\t\t0\t0\thttp://playfusion.com/#intro\t1475688521\t1475899292" 
+1

あなたは '$'と '$ 'を使用しているので、' gsub( "(?<!\ t | ^)\"(?!\ t | $) "、"' "、temp、perl = TRUE)文字クラス内の '^'はアンカーではありません。 –

答えて

1

あなた(?<![^\t])"(?![\t$])正規表現は(そう、"前に文字列のタブまたは開始がなければならない)タブ以外の文字と付いていない、そしてそれはタブまたは$シンボルに従わない"に一致します。

したがって、文字クラス内の^$はアンカーの意味を失います。

交代グループと文字クラスを置き換えます

gsub("(?<!\t|^)\"(?!\t|$)", "'", temp, perl=TRUE) 

(?<!\t|^)後読みは"は、文字列の先頭ではなく、タブが付いていないことが必要です。

(?!\t|$)先読みでは、"が文字列($)の末尾にないことが必要で、タブ文字が続くことはありません。

関連する問題