Python 3.x Python3:索引超出以前工作的脚本的范围
附加的脚本返回: 索引器:列表索引超出范围 对于以值开始的行={line.split(…) 然而,我昨天也可以用它来做同样的事情: 用不同的文本替换xml文件目录中的某些文本Python 3.x Python3:索引超出以前工作的脚本的范围,python-3.x,Python 3.x,附加的脚本返回: 索引器:列表索引超出范围 对于以值开始的行={line.split(…) 然而,我昨天也可以用它来做同样的事情: 用不同的文本替换xml文件目录中的某些文本 import os from distutils.dir_util import copy_tree drc = 'D:/Spielwiese/00100_Arbeitsverzeichnis' backup = 'D:/Spielwiese/Backup/' csv = 'D:/pers
import os
from distutils.dir_util import copy_tree
drc = 'D:/Spielwiese/00100_Arbeitsverzeichnis'
backup = 'D:/Spielwiese/Backup/'
csv = 'D:/persons1.csv'
copy_tree(drc, backup)
values=dict()
with open(csv) as f:
lines =f.readlines()
values = {line.split(',')[0].strip():line.split(',')[1].strip() for line in lines}
#Getting a list of the full paths of files
for dirpath, dirname, filename in os.walk(drc):
for fname in filename:
#Joining dirpath and filenames
path = os.path.join(dirpath, fname)
#Opening the files for reading only
filedata = open(path,encoding="Latin-1").read()
for k,v in values.items():
filedata=filedata.replace(k,v)
f = open(path, 'w',encoding="Latin-1")
# We are writing the the changes to the files
f.write(filedata)
f.close() #Closing the files
print("In case something went wrong, you can find a backup in " + backup)
我没有看到任何奇怪的东西,我可以,如前所述,使用它之前…:-o
有没有办法解决这个问题
致以最良好的祝愿,
K检查csv文件中是否有空行。当我将名称persons1.csv更改为persons.csv时,它会再次工作-有任何提示吗?(名称对我并不重要,但我想知道:-d)这很奇怪,检查你是否仍在阅读同一个文件。还要检查你是否必须避开反斜杠。我唯一更改的是它末尾的1…我甚至没有复制文件,而是直接重命名它…:-D反斜杠?:-D我想Python需要这些?通常我得到C:/path/to/file/(在Win10上)非常奇怪:我刚刚检查了csv的另一个文件名,但这两者都不起作用!
import os
from distutils.dir_util import copy_tree
drc = 'D:/Spielwiese/00100_Arbeitsverzeichnis'
backup = 'D:/Spielwiese/Backup/'
csv = 'D:/persons1.csv'
copy_tree(drc, backup)
values=dict()
with open(csv) as f:
lines =f.readlines()
values = {line.split(',')[0].strip():line.split(',')[1].strip() for line in lines}
#Getting a list of the full paths of files
for dirpath, dirname, filename in os.walk(drc):
for fname in filename:
#Joining dirpath and filenames
path = os.path.join(dirpath, fname)
#Opening the files for reading only
filedata = open(path,encoding="Latin-1").read()
for k,v in values.items():
filedata=filedata.replace(k,v)
f = open(path, 'w',encoding="Latin-1")
# We are writing the the changes to the files
f.write(filedata)
f.close() #Closing the files
print("In case something went wrong, you can find a backup in " + backup)