本文介绍了使用list.files读取R中带重音的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在在这里进入每种状态的R Studio(R 3.1,Windows 8).我在R中使用list.files()函数.

I am reading county geojson files provided here into R Studio (R 3.1, Windows 8) for each of the states. I am using list.files() function in R.

对于州PR,该州有许多带有重音(西班牙语)名称的县. Bayamón.geo.json,Añasco.geo.json.函数list.files()返回缩写形式的文件名,例如An〜asco.geo.json,Bayamo´n.geo.json.

For state PR, which has many counties with accented (Spanish) names viz. Bayamón.geo.json, Añasco.geo.json. The function list.files() returns shortened form of file names like An~asco.geo.json, Bayamo´n.geo.json.

然后在下一步中,我尝试使用上述文件名读取实际文件.我收到这些文件不存在的错误.

And when in the next step I try to read the actual file using above filenames. I get an error that these files don't exist.

我使用的是系统默认编码ISO-8859-1,还尝试将其更改为UTF-8,但没有运气.

I was using system default encoding ISO-8859-1 and also tried changing it to UTF-8, but no luck.

请帮助我解决此问题.如何读取带重音文件名的文件?

Please help me solve this issue. How can I read files with accented filenames?

推荐答案

我遇到了同样的问题,并且我猜想是因为我计算机上的默认系统语言与我想要转换的文件名不同(例如,系统语言=英文) ,文件名=用法语书写).最后,下面的代码帮助我更改了文件名.

I had the same problem and I guess it happened because the default system language on my computer was different from the filenames I wanted to convert (e.g. system language = English, filename = written in french). Finally, the code below helped me to change filenames.

FILENAME_OLD是原始文件的完整路径,例如"C:/directory/file.wav"

FILENAME_OLD is the full path for original files e.g. "C:/directory/file.wav"

FILENAME_NEW是新文件名的完整路径,例如"C:/directory/file_new.wav"

FILENAME_NEW is the full path for new filenames e.g. "C:/directory/file_new.wav"

path = "C:/directory"
setwd(path)

test_old <- Sys.glob('C:/directory/*')
test_new <- gsub("FILENAME_OLD",
                 "FILENAME_NEW", test_old)

file.rename(test_old, test_new)

这篇关于使用list.files读取R中带重音的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!