pands 表头字段自适应

# fields = ['Unnamed: 0', 'Unnamed: 1', '合计:', '人民币元', '美元', '合计:.1', '美元.1', '沙特里亚尔', '合计:.2', '人民币元.1', '美元.2',
#           '沙特里亚尔.1', '合计:.3', '美元.3', '沙特里亚尔.2']
cleaned_sentence = []
a = 0
while a < len(fields):
    current_word = fields[a]
    if current_word == "Unnamed: 0" and len(cleaned_sentence) == 0:
        print("0")

        cleaned_sentence.append(fields[0:fields.index("合计:")])
        a += 1
    elif current_word == "合计:" and len(cleaned_sentence) > 0:
        print("1")

        cleaned_sentence.append(fields[fields.index("合计:"):fields.index("合计:.1")])
        a += 1
    elif current_word == "合计:.1" and len(cleaned_sentence) > 0:
        print("2")
        sub_list = fields[fields.index("合计:.1"):fields.index("合计:.2")]
        # sub_list.append(fields[-1])
        cleaned_sentence.append(sub_list)
        a += 1
    elif current_word == "合计:.2" and len(cleaned_sentence) > 0:
        print("3")
        sub_list = fields[fields.index("合计:.2"):fields.index("合计:.3")]
        # sub_list.append(fields[-1])
        cleaned_sentence.append(sub_list)
        a += 1
    elif current_word == "合计:.3" and len(cleaned_sentence) > 0:
        print("4")
        sub_list = fields[fields.index("合计:.3"):]
        # sub_list.append(fields[-1])
        cleaned_sentence.append(sub_list)
        a += 1
    else:
        a += 1
print(cleaned_sentence)

for each in cleaned_sentence:
    if each[0] == "Unnamed: 0":
        print("find")
        # each = str(each).replace("Unnamed: 0","单位编号")
        each[0] = "单位编号"
        # each = str(each).replace("Unnamed: 1","单位名称")
        each[1] = "单位名称"
        print(each)
    if each[0] == "合计:":
        for i in range(0, len(each)):
            if "合计:" in each[i]:
                # each = str(each).replace("合计:.1", "期初合计")
                each[i] = "期初合计"
            elif "人民币元" in each[i]:
                # each = str(each).replace("人民币元", "期初人民币元")
                each[i] = "期初人民币元"
            elif "美元" in each[i]:
                each[i] = "期初美元"
            elif "沙特里亚尔" in each[i]:
                # each = str(each).replace("沙特里亚尔", "期初沙特里亚尔")
                each[i] = "期初沙特里亚尔"
        print(each)
    if each[0] == "合计:.1":
        for i in range(0, len(each)):
            if "合计:.1" in each[i]:
                # each = str(each).replace("合计:.1", "期初合计")
                each[i] = "本期增加合计"
            elif "人民币元" in each[i]:
                # each = str(each).replace("人民币元", "期初人民币元")
                each[i] = "本期增加人民币元"
            elif "美元" in each[i]:
                each[i] = "本期增加美元"
            elif "沙特里亚尔" in each[i]:
                # each = str(each).replace("沙特里亚尔", "期初沙特里亚尔")
                each[i] = "本期增加沙特里亚尔"
        print(each)
    if each[0] == "合计:.2":
        for i in range(0, len(each)):
            if "合计:.2" in each[i]:
                # each = str(each).replace("合计:.1", "期初合计")
                each[i] = "本期减少合计"
            elif "人民币元" in each[i]:
                # each = str(each).replace("人民币元", "期初人民币元")
                each[i] = "本期减少人民币元"
            elif "美元" in each[i]:
                each[i] = "本期减少美元"
            elif "沙特里亚尔" in each[i]:
                # each = str(each).replace("沙特里亚尔", "期初沙特里亚尔")
                each[i] = "本期减少沙特里亚尔"
        print(each)
    if each[0] == "合计:.3":
        for i in range(0, len(each)):
            if "合计:.3" in each[i]:
                # each = str(each).replace("合计:.1", "期初合计")
                each[i] = "期末合计"
            elif "人民币元" in each[i]:
                # each = str(each).replace("人民币元", "期初人民币元")
                each[i] = "期末人民币元"
            elif "美元" in each[i]:
                each[i] = "期末美元"
            elif "沙特里亚尔" in each[i]:
                # each = str(each).replace("沙特里亚尔", "期初沙特里亚尔")
                each[i] = "期末沙特里亚尔"
        print(each)
print(cleaned_sentence)
list = []
for each in cleaned_sentence:
    list.extend(each)
print(list)  # 整理好的表头

 

你可能感兴趣的:(c#,开发语言,python)