用了30分钟利用java+python给英语老师写了个查看谁没交作业的小程序

前言

突然,当了英语课代表。突然要收100多份作业。啊啊啊,真难啊。

利用Python拿到表格中学生的姓名和学号

import xlrd

excel_path = "F:\大学\学习\大三下\学习资料\英语4\\zhong.xls"
# 打开文件,获取excel文件的workbook(工作簿)对象
excel = xlrd.open_workbook(excel_path, encoding_override="utf-8")

# 获取sheet对象
sheet = excel.sheets()[0]

sheet_row_mount = sheet.nrows  # 4行
sheet_col_mount = sheet.ncols  # 6列

print("列行=",sheet_row_mount, sheet_col_mount)

for x in range(sheet_row_mount):  # 4
    data = "maps.put(\""+sheet.cell_value(x, 0)+"\",\""+sheet.cell_value(x, 1)+"\");"
    print(data)

在这里插入图片描述

利用Java的计算两个map的差集

这里使用的google的一个开源项目guava,如果找不到怎么下载可以私聊我。

package test;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.io.File;
import java.util.*;

public class FileTest {
    private static ArrayList<String> listname = new ArrayList<String>();
    public static void main(String[] args)throws Exception{
        List<Map<String, String>> list = readAllFile("F:\\大学\\学习\\大三下\\学习资料\\英语4\\作业\\模拟1收集");
        Map<String, String> map1 = list.get(0);
        Map<String, String> map2 = list.get(1);
        System.out.println("一共交了:"+map2.size()+"份");
        System.out.println("一共:"+map1.size()+"个人交");
        System.out.println("名单如下:");
        for(String key:map1.keySet()){
            System.out.print(key+":"+map1.get(key)+",");
        }
        System.out.println();
    }

    public static List<Map<String,String>> readAllFile(String filepath) {
        List<Map<String,String>> list = new ArrayList<>();

        File file= new File(filepath);
        Map<String,String> maps = new HashMap<>();

        if(!file.isDirectory()){
            listname.add(file.getName());
        }else if(file.isDirectory()){
            String[] filelist=file.list();
            for(int i = 0;i<filelist.length;i++){
                File readfile = new File(filepath);
                if (!readfile.isDirectory()) {
                    listname.add(readfile.getName());
                } else if (readfile.isDirectory()) {
                    readAllFile(filepath + "\\" + filelist[i]);//递归
                }
            }
        }
        for(int i = 0;i<listname.size();i++){
            String[] split = listname.get(i).split("-");
            maps.put(split[0],split[1]);
//            System.out.println(listname.get(i));
        }
        Map<String, String> guava = getDifferenceSetByGuava(getData(), maps);

        list.add(guava);
        list.add(maps);
        return list;
    }

    /**
     * 自己实现取Map集合的差集--站在巨人的肩膀上造轮子
     *
     * @param bigMap   大集合
     * @param smallMap 小集合
     * @return 两个集合的差集
     */
    private static Map<String, String> getDifferenceSetByGuava(Map<String, String> bigMap, Map<String, String> smallMap) {
        Set<String> bigMapKey = bigMap.keySet();
        Set<String> smallMapKey = smallMap.keySet();
        Set<String> differenceSet = Sets.difference(bigMapKey, smallMapKey);
        Map<String, String> result = Maps.newHashMap();
        for (String key : differenceSet) {
            result.put(key, bigMap.get(key));
        }
        return result;
    }

    public static Map<String,String> getData(){
        Map<String,String> maps = new HashMap<>();
        maps.put("学号","姓名");
    	maps.put("学号","姓名");
        maps.put("学号","姓名");
        return maps;
    }

}

结果:

在这里插入图片描述

发布了144 篇原创文章 · 获赞 154 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_17623363/article/details/104889445