Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:用另一个列表替换一个列表?_Python_List_Rename - Fatal编程技术网

Python:用另一个列表替换一个列表?

Python:用另一个列表替换一个列表?,python,list,rename,Python,List,Rename,我卡住了。我正在将网络上所有具有唯一ID的文件夹移动到中心位置。有几个文件夹有输入错误,因此与中心位置的唯一ID不匹配。我找到了正确的ID,但我需要在移动这些文件夹之前重命名它们。例如,我用错误的唯一ID创建了一个excel电子表格,并且在另一列中使用了正确的ID。现在,我想用正确的ID重命名文件夹,然后将这些文件夹转移到中心位置。我的代码很粗糙,因为我想不出一个好的方法。我觉得使用列表是一种方法,但是由于我的代码是在一个文件夹中迭代的,所以我不确定如何实现这一点 编辑:我想类似的东西可能就是我

我卡住了。我正在将网络上所有具有唯一ID的文件夹移动到中心位置。有几个文件夹有输入错误,因此与中心位置的唯一ID不匹配。我找到了正确的ID,但我需要在移动这些文件夹之前重命名它们。例如,我用错误的唯一ID创建了一个excel电子表格,并且在另一列中使用了正确的ID。现在,我想用正确的ID重命名文件夹,然后将这些文件夹转移到中心位置。我的代码很粗糙,因为我想不出一个好的方法。我觉得使用列表是一种方法,但是由于我的代码是在一个文件夹中迭代的,所以我不确定如何实现这一点

编辑:我想类似的东西可能就是我想要的

例: 在文件夹A中:名为12334的文件应重命名为1234。然后移动到文件夹1234中的基本目录

这是我的密码:

import os
import re
import sys
import traceback
import collections
import shutil

movdir = r"C:\Scans"
basedir = r"C:\Links"
subfolder = "\Private Drain Connections"

try:
    #Walk through all files in the directory that contains the files to copy
    for root, dirs, files in os.walk(movdir):
        for filename in files:
            #find the name location and name of files
            path = os.path.join(root, filename)

            #file name and extension
            ARN, extension = os.path.splitext(filename)
            print ARN

            #Location of the corresponding folder in the new directory
            link = os.path.join(basedir, ARN)
            if not os.path.exists(link):
                newname = re.sub(372911000002001,372911000003100,ARN)
                newname =re.sub(372809000001400,372909000001400,ARN)
                newname =re.sub(372809000001500,372909000001500,ARN)
                newname =re.sub(372809000001700,372909000001700,ARN)
                newname = re.sub(372812000006800,372912000006800,ARN)
                newname =re.sub(372812000006900,372912000006900,ARN)
                newname =re.sub(372812000007000,372912000007000,ARN)
                newname =re.sub(372812000007100,372912000007100,ARN)
                newname =re.sub(372812000007200,372912000007200,ARN)
                newname =re.sub(372812000007300,372912000007300,ARN)
                newname =re.sub(372812000007400,372912000007400,ARN)
                newname =re.sub(372812000007500,372912000007500,ARN)
                newname =re.sub(372812000007600,372912000007600,ARN)
                newname =re.sub(372812000007700,372912000007700,ARN)
                newname =re.sub(372812000011100,372912000011100,ARN)


                os.rename(os.path.join(movdir, ARN, extension ),
                os.path.join(movdir, newname, extension))
                oldpath = os.path.join(root, newname)
                print ARN, "to", newname
                newpath = basedir + "\\" + newname + subfolder
                shutil.copy(oldpath, newpath)
                print "Copied"

except:
    print ("Error occurred")
感谢下面的答案,这是我的最终代码:

import arcpy
import os
import re
import sys
import traceback
import collections
import shutil

movdir = r"C:\Scans"
basedir = r"C:\Links"
subfolder = "\Private Drain Connections"


import string

l = ['372911000002001',
'372809000001400',
'372809000001500',
'372809000001700',
'37292200000800'
]
l2 = ['372911000003100',
'372909000001400',
'372909000001500',
'372909000001700',
'372922000000800'
]

try:
    #Walk through all files in the directory that contains the files to copy
    for root, dirs, files in os.walk(movdir):
        for filename in files:
            #find the name location and name of files
            path = os.path.join(root, filename)

            #file name and extension
            ARN, extension = os.path.splitext(filename)
            oldname = str(ARN)
            #Location of the corresponding folder in the new directory
            link = os.path.join(basedir, ARN)

            if not os.path.exists(link):
                for ii, jj in zip(l, l2):
                    newname = re.sub(ii,jj, ARN)
                    newname = str(newname)
                    print path
                    newpath = os.path.join(root, oldname) +  extension
                    print "new name", newpath
                    os.rename(path, newpath)
                    print "Renaming"
                    newpath2 = basedir + "\\" + newname + subfolder
                    shutil.copy(newpath, newpath2)
                    print "Copied"
                if newname != ARN:
                    break


            else:
                continue

except:
    print ("Error occurred")
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \
        str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
    msgs = "GP ERRORS:\n" + arcpy.GetMessages(2 )+ "\n"
    print (pymsg)
    print (msgs)

想法:尝试将id转换为字符串。我的意思是:

            newname = re.sub('372911000002001','372911000003100',ARN)

希望有帮助

对我来说,方法是将两个列表读入列表对象:

list1 = ["372911000002001", "372809000001400", "372809000001500"]
list2 = ["372911000003100", "372909000001400", "372909000001500"]
for ii, jj in zip(list1, list2):
    newname = re.sub(ii,jj,ARN)  #re.sub returns ARN if no substitution done
    if newname != ARN:
       break

那是。。。不仅仅是替换一个列表…@IgnacioVazquez Abrams我已经完成了代码的另一部分,我需要一种更有效的方法来替换坏名字。高效还是优雅?我们讨论了多少错误的文件夹?@Jblasco目前只有30个,我认为效率不是问题。。。我认为优雅是第一位的;)