pyhton3 CSV某列值转换TXT

pyhton3 CSV某列值转换TXT

#CSV某列值转换TXT
import time
import csv
import hashlib

filtpath="test.csv"

#读取值
def do_readline(row):
    with open(filtpath[0:-4] + "_to.txt","a") as file_handle:       #写入txt
        _value=row[3]
        file_handle.write(_value)                                   # 写入
        file_handle.write("
")                                     # 自动转行

#转md5
def do_md5line(row):
    with open(filtpath[0:-4] + "_md5.txt","a") as file_handle:
        _value=row[3]
        m=hashlib.md5(_value.encode())#变成bytes类型才能加密
        str_md5=m.hexdigest()
        file_handle.write(str_md5)                                 
        file_handle.write("
")                                    
        
with open(filtpath, "r", encoding="utf-8") as f:
  reader = csv.reader(f)
  print(time.strftime("%Y-%m-%d %H:%M:%S") + " 开始..." + filtpath)
  print(type(reader))
  line_num = 0
  for row in reader:
    #print(row[3]) # 第4列结果
    line_num += 1
    #do_readline(row)
    if (line_num != 1): # 第一行标题头去掉
        do_readline(row)
        do_md5line(row)
        print(line_num)
  print(time.strftime("%Y-%m-%d %H:%M:%S") + " 完成..." + filtpath)
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » pyhton3 CSV某列值转换TXT