How can I read 'utf-8' text file in R?

2019-08-15 05:44发布

问题:

I need to read text file in R.

so I used "read.delim()".

It works well when I read text file encoded ASCII

but I can't read UTF-8

so I have to encode the text file to ASCII

how can I encode text files to "ASCII" in R?

the warning message below present when I read utf-8 text file.

It seems file path isn't read

Warning messages:

1: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  invalid input found on input connection 'C:/?곗씠?곕텇??OB留μ</02.INPUT_DB_query_data/POC?곗씠??DRAFT/DRAFT_20110105.txt'
2: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  incomplete final line found by readTableHeader on 'C:/?곗씠?곕텇??OB留μ</02.INPUT_DB_query_data/POC?곗씠??DRAFT/DRAFT_20110105.txt'
3: In read.table(file = file, header = header, sep = sep, quote = quote,  :
  header and 'col.names' are of different lengths

回答1:

Try with encoding='UTF-8'

read.delim(file='filename',
           encoding='UTF-8'
           #Other settings
           )

I hope this will work....



标签: r encoding