2010-02-04 78 views
0
subject score studentid 
xx 23.22% 1 
yy 34% 2 
zz 55.2% 3 
xx 88.66% 4 
yy 23.76% 5 
zz 78.04% 6 

如何获得每个主题的最大百分比和学生ID?从csv获得最大百分比

+2

@stack - 你开始冒险进行实际的数据处理。我强烈推荐一些其他工具,而不是cmd脚本。批处理文件在这种类型的处理中表现不佳的地方太多了。使用vbscript,perl,basic,python - 几乎除了Windows/DOS/cmd脚本之外的任何东西。看到这个答案为指向一本好书对批处理文件以及更多的细节,为什么我建议你使用别的东西:http://stackoverflow.com/questions/180754/best-free-resource-for-learning-高级批处理文件使用情况/ 180767#180767 – 2010-02-04 07:32:19

+0

没有perl但批处理。由于您的脚本在比较时遇到了一些问题......我们如何进行数字比较? – stack 2010-02-04 08:06:28

回答

0

这里是一个VBScript你可以尝试

Set objFS = CreateObject("Scripting.FileSystemObject") 
Set objArgs = WScript.Arguments 
Set d = CreateObject("Scripting.Dictionary") 
Set e = CreateObject("Scripting.Dictionary") 
strFile = objArgs(0) 
Set objFile = objFS.OpenTextFile(strFile) 
Do Until objFile.AtEndOfStream 
    strLine=objFile.ReadLine 
    s = Split(strLine," ") 
    subject =s(0) 
    score= Left(s(1),Len(s(1))-1) 
    studentid=s(2) 
    If Not d.Exists(subject) Then 
     d.Add subject, score 
     e.Add subject, studentid 
    Else 
     If score >= d.Item(subject) Then   
      d.Item(subject) = score 
      e.Item(subject) = studentid 
     End If 
    End If 
Loop 
j=d.Keys 
For Each stritems In j 
    WScript.Echo "Subject:"&stritems & ", Score: "& d.Item(stritems) & "%, StudentID: " & e.Item(stritems) 
Next 

输出

C:\test>type file 
xx 23.22% 1 
yy 34% 2 
zz 55.2% 3 
xx 88.66% 4 
yy 23.76% 5 
zz 78.04% 6 

C:\test>cscript //nologo test.vbs file 
Subject:xx, Score: 88.66%, StudentID: 4 
Subject:yy, Score: 34%, StudentID: 2 
Subject:zz, Score: 78.04%, StudentID: 6 
0

你需要有某种形式的收集处理为每个主题的总数,这可能帮助

@echo off 

    set CountXX=1 
    set CountYY=2 


    set This=XX 
    call :Resolve Count%This% 
    echo %RetVal% 


    set This=YY 
    call :Resolve Count%This% 
    echo %RetVal% 

    set /a Count%This%=%RetVal% + 2 

    call :Resolve Count%This% 
    echo %RetVal% 

goto :eof 


:Resolve 


    for /f "delims== tokens=2" %%a in ('set %1') do set retval=%%a 

goto :eof