如何使用Python从txt文件数据添加C​​SV的第一行中的行名和添加文件名?

时间:2019-01-30 13:20:06

标签: python csv export-to-csv

我正在尝试根据日期明智的文件夹查找用户明智的数据,在多个文件夹中,数据格式将是相同的。

根据user过滤数据后,Date明智地在相同路径中创建用户名文件夹,并以.txt格式存储数据。

我正在尝试将.txt格式转换为.csv格式,但是我无法在csv中创建数据。

  

在txt文件中数据格式如下

02:01:2019:13:43:26:0.10,-0.02,-1.05,-11.88,140.12,-4.58,0.24,-1.89,0.55
     

我需要这样的输出,第一个行用用户名或ID填充:
   I need output Like this with first Rows fill with user-name or id

import os
import datetime
import glob
import re
my_dir = "/home/trail/example/file/"

user_pattern = "user ::[0-9]+"
data_read_pattern = "^[0-9]+:[0-9]+:[0-9]+:[0-9]+:[0-9]+:[0-9]+:(\-?)[0-9]+\.[0-9]+,(\-?)[0-9]+\.[0-9]+,(\-?)[0-9]+\.[0-9]+,(\-?)[0-9]+\.[0-9]+,(\-?)[0-9]+\.[0-9]+,(\-?)[0-9]+\.[0-9]+,(\-?)[0-9]+\.[0-9]+,(\-?)[0-9]+\.[0-9]+,(\-?)[0-9]+\.[0-9]+$"


def ensure_path(path):
  if not os.path.exists(path):
      print "Creating Path", path
      os.makedirs(path)
      os.chmod(path, 0o777)



for i in range(26,-1,-1):
  cur_day = datetime.datetime.now() - datetime.timedelta(days=i)
  cur_dir = my_dir + str(cur_day.year) + "/" + str(cur_day.month) + "/" + str(cur_day.day)
  #print cur_dir
  if os.path.exists(cur_dir):
     all_files = glob.glob(cur_dir+"/dat*.txt")
     all_files.sort(key=os.path.getmtime)
     #print all_files
     op_dir = cur_dir + '/op/'
     ensure_path(op_dir)

     new_file_pointers = {}
     for file_path in all_files:
        with open(file_path, 'r') as f:
            cur_op = None
            cur_f_p = None  
            for line in f:
                if re.match(user_pattern, line):
                    cur_op = re.findall(r'\d+', line)[0]
                    if cur_op in new_file_pointers:
                        print "in if cur_op", cur_op
                        if cur_f_p:
                            cur_f_p.close()
                        cur_f_p =  open(new_file_pointers[cur_op], "a+")
                    else:
                        print "in else cur_op", cur_op
                        if cur_f_p:
                            cur_f_p.close()
                        new_file_pointers[cur_op] = op_dir+str(cur_op)+".txt"
                        cur_f_p = open(new_file_pointers[cur_op], "a+")
                if cur_f_p:
                    if re.match(data_read_pattern, line.rstrip()):
                        cur_f_p.write(line)


for i in range(26,-1,-1):
    cur_day = datetime.datetime.now() - datetime.timedelta(days=i)
    cur_dir = my_dir + str(cur_day.year) + "/" + str(cur_day.month) + "/" + str(cur_day.day)
   #print cur_dir
   if os.path.exists(cur_dir):
    all_files = glob.glob(cur_dir+"/op/*.txt")
    all_files.sort(key=os.path.getmtime)
    print all_files

0 个答案:

没有答案