简体   繁体   English

如何在R中将整个CSV文件的反斜杠替换为正斜杠?

[英]How to replace backslash to forward slash for entire CSV file in R?

I have a simple R script: 我有一个简单的R脚本:

file1 <- read.csv2("D:/Home/file1.csv", strip.white = TRUE, header = FALSE)
file2 <- read.csv2("D:/Home/file2.csv", strip.white = TRUE, header = FALSE)

df <- merge(file1, file2, by.x = c(2), by.y = c(1)) 

df2 <- data.frame(new_col = paste('"', df$V2, '#', df$V1, '#', df$V2.y, '",', sep = ""))

write.table(df2, append = FALSE, file = outFile, sep = "#", quote = FALSE, row.names = FALSE, col.names = FALSE)

File 1 is like this: 文件1是这样的:

100;folder/path/myfile.mp3
101;folder/path/anotherfile.mp3
102;folder/path/finalfile.mp3

File 2 is like this: 文件2如下所示:

folder\path\myfile;64
folder\path\anotherfile;58
folder\path\finalfile;34

So my script merges file 1 with file 2 based on the path column (second column in file 1 and 1st column in file 2). 因此,我的脚本基于path列(文件1中的第二列和文件2中的第一列)将文件1与文件2合并。 It does this fine if both files have forward slashes for each row. 如果两个文件的每一行都带有正斜杠,则可以做到这一点。

The problem is that file 1 has forward slashes and file 2 has backslashes so the merge isn't working. 问题在于文件1具有正斜杠,文件2具有反斜杠,因此合并无法正常进行。

How do I make it so that the merge will work given that they both use different slashes? 我如何做到这一点,因为它们都使用不同的斜杠,合并将起作用? In other words, how can I convert all of file2 to use forward slashes prior to the merge? 换句话说,如何在合并之前将所有file2转换为使用正斜杠? I need the final result to use forward slashes, not backslashes. 我需要最终结果使用正斜杠,而不是反斜杠。

I have looked through lots of other questions and answers and replacing backslashes to forward slashes has been asked before but only on strings. 我研究了许多其他问题和答案,之前曾有人问过将反斜杠替换为正斜杠,但仅针对字符串。 I can't find a question asking how to replace every slash in the whole source CSV file. 我找不到一个问题,询问如何替换整个源CSV文件中的每个斜杠。 So I don't believe this is a duplicate. 所以我不认为这是重复的。

Many thanks. 非常感谢。

This should work: 这应该工作:

file2$column = gsub(pattern = "\\\\", replacement = "/", x = file2$column)

Replace column in my code with whatever the column name is. 替换column在我与任何列名称代码。

Another regex could be the following. 另一个正则表达式可能如下。

x <- 'a\\b\\c'
gsub('[\\]', '/', x)
#[1] "a/b/c"

Or, using argument fixed = TRUE , 或者,使用参数fixed = TRUE

gsub('\\', '/', x, fixed = TRUE)
#[1] "a/b/c"

Now it's a matter of applying the above to the column(s) of the dataframe. 现在,只需将以上内容应用于数据框的列即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM