Python - 从CSV文件中去除所有驱动器字母并替换为Z:
这里有一个代码示例。基本上,output.csv 文件需要去掉任何驱动器字母 A: 到 Y: 的部分,并把它们替换成 Z:。我尝试用一个列表来做到这一点(还没完成),但出现了一个错误:TypeError: expected a character buffer object。
#!/usr/bin/python
import os.path
import os
import shutil
import csv
import re
# Create the videos directory in the current directory
# If the directory exists ignore it.
#
# Moves all files with the .wmv extenstion to the
# videos folder for file structure
#
#Crawl the videos directory then change to videos directory
# create the videos.csv file in the videos directory
# replace any drive letter A:-Y: with Z:
def createCSV():
directory = "videos"
if not os.path.isdir("." + directory + "/"):
os.mkdir("./" + directory + "/")
for file in os.listdir("./"):
if os.path.splitext(file)[1] == ".wmv":
shutil.move(file, os.path.join("videos", file))
listDirectory = os.listdir("videos")
os.chdir(directory)
f = open("videos.csv", "w")
f.writelines(os.path.join(os.getcwd(), f + '\n') for f in listDirectory)
f = open('videos.csv', 'r')
w = open('output.csv', 'w')
f_cont = f.readlines()
for line in f_cont:
regex = re.compile("\b[GHI]:")
re.sub(regex, "Z:", line)
w.write(line)
f.close()
createCSV()
编辑:
我觉得我的思路/逻辑有问题,生成的 output.csv 文件里仍然有 G:,没有从 re.sub 这一行改成 Z:\。
3 个回答
0
你可以使用
for driveletter in removedrives:
line = line.replace(driveletter, 'Z:')
这样就可以遍历你的列表,并一个一个地替换可能的驱动器字母。正如abyx所说,replace
需要的是字符串,而不是列表,所以你需要这个额外的步骤。
或者可以使用正则表达式,比如
import re
regex = re.compile(r"\b[FGH]:")
re.sub(regex, "Z:", line)
额外的好处是:正则表达式可以检查这确实是一个驱动器字母,而不是像OH: hydrogen group
这样更大部分的内容。
除此之外,我建议你使用os.path
自带的路径处理函数,而不是自己去实现这些功能。
当然,如果你还要对CSV文件做其他操作,记得看看csv
模块。
之前有评论提到,你应该关闭所有打开的文件。或者使用with
语句:
with open("videos.csv", "w") as f:
do_stuff()
1
看起来问题出在你代码底部的那个循环里。字符串的 replace
方法并不是接收一个列表作为第一个参数,而是需要另一个字符串。你需要遍历你的 removeDrives
列表,然后用列表里的每一个项目去调用 line.remove
。
1
我看到你使用了一些很有“Python风格”的代码片段,比如聪明地使用了path.join和注释的代码。这些可以做得更好,我们来重写一些内容,这样不仅能解决你关于驱动器字母的问题,还能让代码更符合Python的风格:
#!/usr/bin/env python
# -*- coding= UTF-8 -*-
# Firstly, modules can be documented using docstring, so drop the comments
"""
Create the videos directory in the current directory
If the directory exists ignore it.
Moves all files with the .wmv extension to the
videos folder for file structure
Crawl the videos directory then change to videos directory
create the videos.csv file in the videos directory
create output.csv replace any drive letter A:-Y: with Z:
"""
# not useful to import os and os.path as the second is contain in the first one
import os
import shutil
import csv
# import glob, it will be handy
import glob
import ntpath # this is to split the drive
# don't really need to use a function
# Here, don't bother checking if the directory exists
# and you don't need add any slash either
directory = "videos"
ext = "*.wmv"
try :
os.mkdir(directory)
except OSError :
pass
listDirectory = [] # creating a buffer so no need to list the dir twice
for file in glob.glob(ext): # much easier this way, isn't it ?
shutil.move(file, os.path.join(directory, file)) # good catch for shutil :-)
listDirectory.append(file)
os.chdir(directory)
# you've smartly imported the csv module, so let's use it !
f = open("videos.csv", "w")
vid_csv = csv.writer(f)
w = open('output.csv', 'w')
out_csv = csv.writer(w)
# let's do everything in one loop
for file in listDirectory :
file_path = os.path.abspath(file)
# Python includes functions to deal with drive letters :-D
# I use ntpath because I am under linux but you can use
# normal os.path functions on windows with the same names
file_path_with_new_letter = ntpath.join("Z:", ntpath.splitdrive(file_path)[1])
# let's write the csv, using tuples
vid_csv.writerow((file_path, ))
out_csv.writerow((file_path_with_new_letter, ))