所以我在目录中有一些。txt文件。每个。txt文件包含如下路径:
'C:dfolderprojectfolderFolder1Folder2Folder3Module.c'
'C:dfolderprojectfolderFolder1Folder2Folder3Module2.c'
'C:dfolderprojectfolderFolder1Folder2Folder3Module3.c'
我只需要一些小函数,它将遍历每个文件的每一行,并删除其中的',因此只剩下clear path,如:
C:dfolderprojectfolderFolder1Folder2Folder3Module.c
C:dfolderprojectfolderFolder1Folder2Folder3Module2.c
C:dfolderprojectfolderFolder1Folder2Folder3Module3.c
我现在的代码是:
for filename in files:
with open(filename, 'r') as file:
content = file.read().split('n')
for line in content:
if line.startswith('')and line.endswith(''):
remove('')
请协助!
解决方案:
我已经设法用一种不同的方法找到了解决方案:
for filename in files:
f = open(filename, 'rt')
filedata = f.read()
filedata = filedata.replace("'","")
f.close()
f = open(filename, 'wt')
f.write(filedata)
f.close()
谢谢!
python对字符串'
,"
,""
等有层次结构,因此您可以将一个加号包装成分隔引号。因为第一个元素''
在刻度之前,所以第二个元素是路径
line.split("'")[1]
编辑:如果我没理解错的话,你想要这个
for filename in files:
paths = []
with open(filename, 'r') as file:
content = file.read().split('n')
for line in content:
paths.append(line.split("'")[1])
file.close()
with open(filename, 'w') as file:
file.writelines(paths)
file.close()
所以我只是用了一点不同的方法,并设法找到了一个解决方案:
for filename in files:
f = open(filename, 'rt')
filedata = f.read()
filedata = filedata.replace("'","")
f.close()
f = open(filename, 'wt')
f.write(filedata)
f.close()
无论如何,谢谢大家!