import json # 将从hue中copy过来的fields转换为固定格式的字符串 def get_extract_fields(copy_str): filed_arr = copy_str.split("\n") with_comma = "" no_comma = "" with_comma_no_backspace = "" special = "" for k in range(len(filed_arr)): fa = filed_arr[k] field = fa.split(" ")[0] no_comma += "%s\r\n" % field with_comma += "%s,\r\n" % field with_comma_no_backspace += "%s," % field special += "coalesce(t2.%s,t1.%s) %s,\r\n" % (field, field, field) return [no_comma, with_comma, with_comma_no_backspace, special] # Press the green button in the gutter to run the script. if __name__ == '__main__': # 直接复制json数据赋值json_str filed_str = """fid (bigint) fuserid (bigint) froomid (bigint) ftype (int) fcount (int) frecordtime (timestamp) fcreatetime (timestamp) fmodifytime (timestamp) """ print(get_extract_fields(filed_str))