[英]Create text file of specific format from excel/csv input in r
我需要生成具有特定格式的文本文件,它應該從 excel/csv 文件中獲取輸入,其中包含數千行條目來創建文本 output 文件。
我的輸入示例 Excel/csv 輸入如下所示
tibble::tibble(location = c(148,85),
proxy_pass = c("http://110.140.110.105/",
"http://110.140.110.110/"))
我的 output 文本文件格式如下:
location /148/ {
proxy_pass http://110.140.110.105/;
proxy_set_header Authorization "xyz";
}
location /85/ {
proxy_pass http://110.140.110.110/;
proxy_set_header Authorization "xyz";
}
位置和 proxy_pass 之后的文本通過從 Excel/CSV 中選取值而發生變化。 我可以在 R 中進行此轉換嗎? 如果有人有解決方案,請幫助我。
您可以首先使用purrr
中的map2
創建模式,然后將列表寫入文本文件。
list_proxy <- purrr::map2(df$location, df$proxy_pass, ~paste0("location /", .x, "/ {
proxy_pass ", .y, ";
proxy_set_header Authorization 'xyz';
}"))
lapply(list_proxy, write, "proxy.txt", append=TRUE)
Output :
location /148/ {
proxy_pass http://110.140.110.105/;
proxy_set_header Authorization 'xyz';
}
location /85/ {
proxy_pass http://110.140.110.110/;
proxy_set_header Authorization 'xyz';
}
資料:
df <- tibble::tibble(location = c(148,85),
proxy_pass = c("http://110.140.110.105/",
"http://110.140.110.110/"))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.