csv文件去掉最后的逗号,CSV数据提取末尾的逗号

a little background on my problem:

I have CSV file that is created by a query in MS Access. The main body of the CSV file contains columns; names, address, post codes, dates, etc. The last line of the file contains a summary of the data; filename, date, timestamp and total number of records. As all fields contain strings and we did not want the system to truncate or try and evaluate the dates and post codes, we've output these fields with quotation marks.

Last two lines look something like this:

"Name","Code","Address","Address2","Office","Location","City","State","PostCode","Date"

"FileName","DDMMYYYY","HH:MM:SS",TotalRecords,"","","","","",""

I need to remove the extra commas in the final line so it looks like this:

"FileName","DDMMYYYY","HH:MM:SS",TotalRecords

I've had a look through and tried the solution here:

CSV file has extra commas

This creates a copy of my original file but leaves all the commas intact. This solution works if I remove ALL the quotation marks.

解决方案

Thanks everyone for your help.

I've used a different route to solve my problem. I created a batch script that extracts the records and reprint the data into a new file, ommitting the extra commas at the end of the file.

My code is below, any critique to improve it, is appreciated.

echo off

setlocal EnableDelayedExpansion

pause

set cur=0

FOR /F "delims=" %%A in (input.csv) DO (

set line=%%A

set line=!line:,,=, ,!

FOR /F "tokens=1-11 delims=," %%G in (^"!line!^") DO (

if "%%G"=="" (echo.)

if "%%G"==""FILENAME"" (

echo %%G,%%H,%%I,%%J >> test_%date:~-4,4%%date:~-7,2%%date:~-10,2%.csv

goto EOF

) else (

echo %%G,%%H,%%I,%%J,%%K,%%L,%%M,%%N,%%O,%%P,%%Q >> test_%date:~-4,4%%date:~-7,2%%date:~-10,2%.csv

)

set /a cur=cur+1

)

)

:EOF

echo %cur%

pause

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值