文本替换使用CSV数据 [英] Text replacement using csv data
本文介绍了文本替换使用CSV数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个参数,第一个是一个文本文件,第二个是一个CSV文件。
第一个文件是这样的:
从(地址)(姓名)是(年龄)岁。
第二个文件是这样的:
姓名,地址,年龄:詹姆斯,伦敦,34亚历克斯,25日,纽约
我要取代这个:(名)詹姆斯(地址)(伦敦)和(年龄)到34等等......
我要输出这样的:詹姆斯从伦敦是34岁
等...
所以,我怎么能csv文件的存储DATAS在多维数组?
像这样$ DATA [行数] [列号]
例如$数据[1] [0]应该是:亚历
我的code:
$参数=的$ args [0]
$数据=的$ args [1]为($ i = 1; $ I $ -lt data.lenght / 3; $ I ++)
{
$ parameter.replace('(名称)','???')|设置内容$参数
$ parameter.replace('(地址),???)|设置内容$参数
$ parameter.replace('(岁),???)|设置内容$参数
#我想输出$参数}
解决方案
尝试导入-CSV
示例
$数据=C:\\用户\\ testuser的\\桌面\\ test.csv$名单=进口-CSV $数据$列表
$列表[0] .Name点
I have 2 arguments, the first is a text file, and a second is a csv file.
The first file look like:
(name) from (address) is (age) years old.
The second file look like:
Name,Address,Age
James,London,34
Alex,New York,25
I want to replace this: (name) to James, (address) to (London) and (age) to 34 etc...
I want to output this: James from London is 34 years old.
etc...
So how can I store the datas of csv file in a multidimensional array? Like this $data[line number][column number] For example the $data[1][0] should be: Alex
My code:
$parameter = $args[0]
$data = $args[1]
for($i = 1; $i -lt $data.lenght/3; $i++)
{
$parameter.replace('(name)', '???') | Set-Content $parameter
$parameter.replace('(address)', '???') | Set-Content $parameter
$parameter.replace('(age)', '???') | Set-Content $parameter
#I want to output $parameter
}
解决方案
Try Import-Csv
Example
$data = "C:\Users\testuser\Desktop\test.csv"
$list = Import-Csv $data
$list
$list[0].Name
这篇关于文本替换使用CSV数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文