包括内容: 1、读取文件数据(open方法)
2、Python的迭代器使用? ?
3、文件的写入?
4、判断表头是否合理
# xjy_create
# 读取csv文件
line_length = 0
with open(filename, 'r')as read_csv:
print("open filename as read_csv")
# 读取5行数据
lines = read_csv.readlines(5)
flag = True # 创建一个判断值:True or False
# 创建一个迭代器
for index in range(len(lines)):
line = lines[index].split(',') # 将每一行从 str -> list
if index == 0:
line_length = len(line) # 每一行的元素个数
print ("====line_length is:===")
print line_length
else:
if line_length != len(line):
flag = False
break
# 判断是一个合理的表格: 表头每一列个数都相同
if flag:
print ("if flag == True")
# 将表头存入内存
file_header = "/home/liuyy/dataImport/data/" + str(user_id) + " " + tbname + ".ctl"
with open(filename, 'r')as read_csv:
with open(file_header, 'w') as store_header:
print ("===store table_header===")
# 读取csv文件前五行 并写入内存
test_line = 0
for line in read_csv.readlines():
test_line = test_line + 1
# print line
if test_line <= 5:
store_header.write(line)
else:
break
# 入库操作
with open("/home/liuyy/dataImport/dataImport/loader_new.sh", 'r')as f_in:
print ("===loader_new.sh===")
with open(file4, 'w')as f_out:
for line in f_in.readlines():
print ("****loader.sh : line***")
print line
if line.find("[ctl]") >= 0 or line.find("[log]") >= 0:
f_out.write(line.replace("[ctl]", file1).replace("[log]", log))
else:
f_out.write(line)
# xjy_create_end
|