我有一个 CSV 文件,其中包含单个标题记录和字段数据:
region,travelnum,seatnum,1.Stat.Money.Min,1.Stat.Money.Avg,1.Stat.Money.Med,1.Stat.Money.Max,1.Stat.Money.Std,1.Stat.Family.Min,1.Stat.Family.Avg,1.Stat.Family.Med,1.Stat.Family.Max,1.Stat.Family.Std,1.Stat.Travel.Min,1.Stat.Travel.Avg,1.Stat.Travel.Med,1.Stat.Travel.Max,1.Stat.Travel.Std,1.Stat.Job.Min,1.Stat.Job.Avg,1.Stat.Job.Med,1.Stat.Job.Max,1.Stat.Job.Std
A and B,324,8714,0.48,(...),
C and D,199,1733,0.32,(...),
E and F,915,2501,0.72,(...),
...
数字“1”对应于主题的“名称”,其中每个主题都有自己的文件夹和此 .csv 文件。对于主题 2,标题从 1 更改为 2。想法是将包含很多行的 .csv 重新排列为一行用于标题,另一行用于值但有很多列。为了更简单的数据框...
应该是什么:
name,A_and_B_travelnum,A_and_B_seatnum,A_and_B_Money_Min,A_and_B_Money_Avg,A_and_B_Money_Med,A_and_B_Money_Max,A_and_B_Money_Std,A_and_B_Family_Min,A_and_B_Family_Avg,A_and_B_Family_Med,A_and_B_Family_Max,A_and_B_Family_Std,A_and_B_Travel_Min,A_and_B_Travel_Avg,A_and_B_Travel_Med,A_and_B_Travel_Max,A_and_B_Travel_Std,A_and_B_Job_Min,A_and_B_Job_Avg,A_and_B_Job_Med,A_and_B_Job_Max,A_and_B_Job_Std,C_and_D_travelnum,C_and_D_seatnum,C_and_D_Money_Min,C_and_D_Money_Avg,C_and_D_Money_Med,C_and_D_Money_Max,C_and_D_Money_Std,C_and_D_Family_Min,C_and_D_Family_Avg,C_and_D_Family_Med,C_and_D_Family_Max,C_and_D_Family_Std,C_and_D_Travel_Min,C_and_D_Travel_Avg,C_and_D_Travel_Med,C_and_D_Travel_Max,C_and_D_Travel_Std,C_and_D_Job_Min,C_and_D_Job_Avg,C_and_D_Job_Med,C_and_D_Job_Max,C_and_D_Job_Std,...
1,324,8714,0.48,(...),199,1733,0.32,(...),...
我做了什么:
@echo off
setlocal EnableDelayedExpansion EnableExtensions
set "region[A and B]=A_and_B"
set "region[C and D]=C_and_D"
set "region[E and F]=E_and_F"
set "metric[.Stat.Money.Min]=Money_Min"
set "metric[.Stat.Money.Avg]=Money_Avg"
set "metric[.Stat.Money.Med]=Money_Med"
set "metric[.Stat.Money.Max]=Money_Max"
set "metric[.Stat.Money.Std]=Money_Std"
set "metric[.Stat.Family.Min]=Family_Min"
set "metric[.Stat.Family.Avg]=Family_Avg"
set "metric[.Stat.Family.Med]=Family_Med"
set "metric[.Stat.Family.Max]=Family_Max"
set "metric[.Stat.Family.Std]=Family_Std"
set "metric[.Stat.Travel.Min]=Travel_Min"
set "metric[.Stat.Travel.Avg]=Travel_Avg"
set "metric[.Stat.Travel.Med]=Travel_Med"
set "metric[.Stat.Travel.Max]=Travel_Max"
set "metric[.Stat.Travel.Std]=Travel_Std"
set "metric[.Stat.Job.Min]=Job_Min"
set "metric[.Stat.Job.Avg]=Job_Avg"
set "metric[.Stat.Job.Med]=Job_Med"
set "metric[.Stat.Job.Max]=Job_Max"
set "metric[.Stat.Job.Std]=Job_Std"
for /d %%y in (*) do (
set id=%%y
rem set nameDIR=%parentfolder%\!name!
call :identification
)
goto EOF:
:identification
set /p "file=" < "!id!\name.csv"
> !id!\name.csv (
for /f "skip=0 tokens=* delims=," %%x in ("!id!\name.csv")
echo name,!region[A and B]!_travelnum,!region[A and B]!_seatnum,!region[A and B]!!metric[.Stat.Money.Min]!...
for /f "usebackq skip=1 tokens=1,* delims=," %%a in ("!id!\name.csv") do (
echo !id!,%%a,%%a,%%a,...
)
)
我不知道如何在 for 循环中连接第二行中的其他值。
你的问题很混乱。你应该更清楚地解释它并包括更多的输入和输出示例数据......无论如何,这是我的版本我认为你要求:
@echo off
setlocal DisableDelayedExpansion
set "name[A and B]=1"
set "name[C and D]=2"
set "name[E and F]=3"
set "reg[1]=A_and_B"
set "reg[2]=C_and_D"
set "reg[3]=E_and_F"
for /F "tokens=1-3* delims=," %%a in (name.csv) do (
set "header=name,!reg[#n#]!_%%b,!reg[#n#]!_%%c,%%d"
goto break
)
:break
set "header=%header:1.Stat=!reg[#n#]!%"
set "header=%header:.=_%"
setlocal EnableDelayedExpansion
set "header=!header:#n#=%%n!"
del result.csv body.txt 2>NUL
< NUL (
for /F "skip=1 tokens=1* delims=," %%a in (name.csv) do (
for %%n in (!name[%%a]!) do (
set /P "=%header%," >> result.csv
set /P "=!name[%%a]!,%%b" >> body.txt
)
)
)
echo/>> result.csv
type body.txt >> result.csv
type result.csv
输出:
name,A_and_B_travelnum,A_and_B_seatnum,A_and_B_Money_Min,A_and_B_Money_Avg,A_and_B_Money_Med,A_and_B_Money_Max,A_and_B_Money_Std,A_and_B_Family_Min,A_and_B_Family_Avg,A_and_B_Family_Med,A_and_B_Family_Max,A_and_B_Family_Std,A_and_B_Travel_Min,A_and_B_Travel_Avg,A_and_B_Travel_Med,A_and_B_Travel_Max,A_and_B_Travel_Std,A_and_B_Job_Min,A_and_B_Job_Avg,A_and_B_Job_Med,A_and_B_Job_Max,A_and_B_Job_Std,name,C_and_D_travelnum,C_and_D_seatnum,C_and_D_Money_Min,C_and_D_Money_Avg,C_and_D_Money_Med,C_and_D_Money_Max,C_and_D_Money_Std,C_and_D_Family_Min,C_and_D_Family_Avg,C_and_D_Family_Med,C_and_D_Family_Max,C_and_D_Family_Std,C_and_D_Travel_Min,C_and_D_Travel_Avg,C_and_D_Travel_Med,C_and_D_Travel_Max,C_and_D_Travel_Std,C_and_D_Job_Min,C_and_D_Job_Avg,C_and_D_Job_Med,C_and_D_Job_Max,C_and_D_Job_Std,name,E_and_F_travelnum,E_and_F_seatnum,E_and_F_Money_Min,E_and_F_Money_Avg,E_and_F_Money_Med,E_and_F_Money_Max,E_and_F_Money_Std,E_and_F_Family_Min,E_and_F_Family_Avg,E_and_F_Family_Med,E_and_F_Family_Max,E_and_F_Family_Std,E_and_F_Travel_Min,E_and_F_Travel_Avg,E_and_F_Travel_Med,E_and_F_Travel_Max,E_and_F_Travel_Std,E_and_F_Job_Min,E_and_F_Job_Avg,E_and_F_Job_Med,E_and_F_Job_Max,E_and_F_Job_Std,
1,324,8714,0.48,(...),2,199,1733,0.32,(...),3,915,2501,0.72,(...),