从csv文件的1列中删除前导零

时间:2015-04-07 16:02:25

标签: java csv formatting string-formatting

我正在寻找一种快速方法从CSV文件的第一列中删除前导零,以便其他程序很好地使用它。如果可能的话,将它从逗号分隔转换为制表符分隔符也很棒,但这可能是一个单独的问题。

这里是标题名称(只需要从学生ID中删除前导(2)零):

"Student ID","School Name","Contact Relation","Contact First Name","Contact Last Name","Salutation"," Home Phone Number","Address Line 1","Address Line 2","City","State","Zip","Zip + 4","Work Phone","Cell Phone","Pager","Residential Status","Custodial Status"

以下是一些示例数据,以了解它的格式:

"004000031","Local High School","Mother","FirstName","LastName","","555-5557773","555 Old Midlothian Tpke","Lot77","Richmond","VA","23222","","","","","","Y"

3 个答案:

答案 0 :(得分:0)

这一行应该这样做:

String[] tokens = String.join("\t", csv.replaceFirst("^\"0*", "\"").split(","));

答案 1 :(得分:0)

修改了生成CSV文件的XML代码。只需添加/修改2行

 <field id="stdIDLocal" header-source="field" header-value="Student ID"     find="00" replaceFirst="" /> 

要将输出从CSV转换为制表符分隔文件,请在标记中使用以下XML语句:

<export table-id="tblStudent" header-row="true" value-delimiter="&#09;" > 

答案 2 :(得分:-1)

这个perl应该为你做。

第一个表达式删除每行的前两个零。 第二个将,更改为标签。

perl -pne 's/^00//;s/,/\t/g' your_file.txt