用变量值重命名当前文件

用变量值重命名当前文件

本文介绍了用变量值重命名当前文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想获取两行相交处的列' ID '的值(遇到 Name Zip 同一行),然后使用该行号,获得" ID "列的值来重命名文件.我不认为为一个正确地写' .rename '..任何指针表示赞赏.当前没有错误,但也没有输出.

I am simply trying to get the value of column 'ID' where two rows meet (meet where Name and Zip are on the same row) then with that row number, I get column 'ID' value to rename file with. I don't think writing the '.rename' correctly for one.. any pointers appreciated. Currently no error, but no output either.

import PyPDF2
from PyPDF2 import PdfFileWriter, PdfFileReader

import re
import config
import xlrd
import numpy as np
import pandas as pd
import math
import os

for filename in os.listdir(config.Total):
    if filename.endswith(".pdf"):
        First_Name, Last_Name, Zip = filename.replace(".pdf",'').split()
        Name = First_Name + " " + Last_Name

        print(Name)
        print(Zip)
        data1 = pd.read_excel(config.Excel1)
        data2 = pd.read_excel(config.Excel2)

        df = pd.DataFrame(data1)
        header = df.iloc[0]
        df2 = pd.DataFrame(data2)
        header2 = df2.iloc[0]

        df = df[1:]
        df.rename(columns = header)
        df2 = df2[1:]
        df2.rename(columns = header2)

        row_numberd1 = df[df['Member Name'].str.contains(Name)].index.min()
        row_numberd12 = df[df['Member Address Line 3'].str.contains(Zip)].index.min()

        if row_numberd1 == row_numberd12: # When rows match of NameUp and Zip var in DF1
            rowMatched = row_numberd1
            print("Match Found")
            print(rowMatched)

            MemberID = df['ID'][rowMatched]
            MemberI = str(MemberID)

            os.rename(config.ID+ "/" + MemberI)

        row_numberd2 = df2[df2['Member Name'].str.contains(Name)].index.min()
        row_numberd22 = df2[df2['Member Address Line 3'].str.contains(Zip)].index.min()

        if row_numberd2 == row_numberd22: # When rows match of NameUp and Zip var in DF2
            rowMatched2 = row_numberd2
            print("No Match Found")
            print(rowMatched2)

            MemberID = df2['ID'][rowMatched2]
            MemberI = str(MemberID)

            os.rename(config.ID+ "/" + MemberI)

也许吗?

    os.rename(config.Total, + MemberI, config.ID)

推荐答案

由于代码运行时没有错误,因此绝不能进入if语句的主体.因此,永远不要重命名.也许没有比赛!您可以通过显式重命名来找出答案:

Since the code runs without an error, it must never gets into the body of if statement. Hence never renames. Maybe there is no match! You can find out by explicit renaming:

row = df[df['Member Name'].str.contains(Name) and df['Member Address Line 3'].str.contains(Zip)].index.min()
os.rename("old_filename",str(df['ID'][row]))

这篇关于用变量值重命名当前文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:10