You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将生物识别数据处理Python代码转换为可运行C#代码求助

生物识别数据处理Python代码转C#实现

原Python代码的核心逻辑是:

  • 读取考勤文件并生成备份,跳过第一行数据
  • 逐行解析考勤记录,筛选出与当前年月一致且**日期处于当前上下半月(15号为分界)**的记录,输出或准备入库

以下是对应的C#实现,已处理文件操作、日期解析等常见问题:

using System;
using System.IO;
using System.Linq;

class BiometricDataProcessor
{
    static void Main()
    {
        // 1. 获取当前本地时间,用于备份文件名
        string currentTimeStr = DateTime.Now.ToString("MM-dd-yyyy hh:mm:ss tt");
        
        // 确保backup目录存在,避免写入失败
        Directory.CreateDirectory("backup");
        
        // 2. 读取AGL_001.txt,跳过第一行,写入备份和converted.txt
        string[] allLines = File.ReadAllLines("AGL_001.txt");
        if (allLines.Length > 1)
        {
            // 跳过第一行,取剩余内容
            string[] linesToWrite = allLines.Skip(1).ToArray();
            
            // 写入备份文件
            string backupPath = Path.Combine("backup", $"converted {currentTimeStr}.txt");
            File.WriteAllLines(backupPath, linesToWrite);
            
            // 写入converted.txt
            File.WriteAllLines("converted.txt", linesToWrite);
        }
        
        // 3. 读取converted.txt并处理每一行
        string[] convertedLines = File.ReadAllLines("converted.txt");
        DateTime now = DateTime.Now;
        
        // 确定当前上下半月的日期范围
        int startDay = now.Day >= 16 ? 16 : 1;
        int endDay = now.Day >= 16 ? DateTime.DaysInMonth(now.Year, now.Month) : 15;
        
        foreach (string line in convertedLines)
        {
            // 拆分每行数据,过滤空字符串(避免多余空格导致的索引错误)
            string[] parts = line.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length < 7) continue; // 跳过格式不正确的行
            
            // 提取关键数据
            string userId = parts[2];
            string logDateStr = parts[5];
            string logTimeStr = parts[6];
            
            // 解析日志日期(替换为你的实际日期格式,原代码默认是MM/dd/yyyy)
            if (!DateTime.TryParseExact(logDateStr, "MM/dd/yyyy", null, System.Globalization.DateTimeStyles.None, out DateTime logDate))
            {
                continue; // 解析失败的行直接跳过
            }
            
            // 4. 匹配年月+判断是否在当前上下半月范围内
            if (logDate.Year == now.Year && logDate.Month == now.Month)
            {
                if (logDate.Day >= startDay && logDate.Day <= endDay)
                {
                    // 输出符合条件的记录,对应原Python的print(logs)
                    Console.WriteLine($"({null}, {userId}, {logDateStr}, {logTimeStr})");
                    
                    // 如果要入库,这里可以添加数据库插入逻辑
                    // 例如使用SqlCommand.ExecuteNonQuery等
                }
            }
        }
    }
}

关键差异说明(针对刚学C#的开发者)

  1. 文件操作:C#用File类静态方法更简洁,Directory.CreateDirectory确保备份目录存在,避免Python中可能出现的目录不存在报错
  2. 日期处理:直接用DateTime类型解析和比较,比Python拆分字符串的方式更可靠,避免字符串比较的bug(比如"09"和"8"的字符串比较逻辑错误)
  3. 字符串拆分:用StringSplitOptions.RemoveEmptyEntries过滤多余空格,防止拆分后出现空元素导致索引越界
  4. 上下半月判断:用DateTime.DaysInMonth自动处理不同月份的天数(比如2月、30天的月份),不用硬写31

注意事项

  • 确保AGL_001.txt的格式和原Python代码处理的一致,每行拆分后至少有7个元素
  • 日志日期的格式要和TryParseExact中的格式字符串匹配(如果你的实际格式不同,需要修改参数)
  • 如果需要数据库插入,记得引用System.Data.SqlClient或对应的ORM库,替换注释部分的代码

内容的提问来源于stack exchange,提问作者Louie Jay Orellana Jayme

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 13:00:57