Python:将一个列表替换为另一个列表?

地理信息系统

我被卡住了。我正在网络上四处移动文件夹,这些文件夹都有唯一的ID到中心位置。有一些带有错别字的文件夹,因此在中心位置与唯一ID不匹配。我找到了正确的IDS,但是在移动它们之前,我需要重命名这些文件夹。例如,我创建了一个具有错误的唯一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)
耶布拉斯科

对我而言,方法是将两个列表都读入列表对象:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用jQuery将一个列表替换为另一个列表

将列表中的项目替换为另一个列表中的相应项目(Python)

方案将列表中的一个符号替换为另一个符号

根据名称将列表元素替换为另一个列表元素

将一个类的列表转换为另一个类的列表?

将一个列表转换为另一个列表

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

如何将数组列表中的元素替换为另一个?

查找将Python中的一个集群列表转换为另一个集群列表的映射

Python将字典列表转换为另一个字典列表

根据python中的另一个列表将列表中的字母转换为数字

批处理以将文本列表的某些部分替换为另一个文本列表中指定的替换

如何将特定字符串值替换为python列表中的另一个值?

Java流:将一个对象的列表转换为另一个

将列表从一个类转换为另一个类

根据条件将嵌套列表中的值替换为另一个嵌套列表中的值

根据几种情况,将列表中的项目替换为另一个项目(小列表)

不使用字典将列表中的项目替换为另一个列表中的项目

用另一个列表将名称替换为列表元素

将列表元素转换为另一个列表的列表值

Python:用列表中另一个列表中的值替换列表中的元素

python 从另一个列表中替换两个列表数据

将列表编号转换为另一个返回的值[python]

如何一次遍历两个列表列表并将一个列表中的值替换为另一个列表?

将一个列表中的变量插入/替换到另一个列表中

将列表追加到另一个列表

将列表排序到另一个列表

使用一个列表替换另一个列表中的值

从另一个列表替换一个列表中间的值