Python - 从csv文件中删除所有驱动器号并用Z替换:

时间:2009-11-23 15:48:07

标签: python

这是代码示例。基本上output.csv需要删除任何驱动器号A:-Y:并用Z替换它:我试图用列表(尚未完成)执行此操作但它生成错误: TypeError:期望一个字符缓冲区对象

#!/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:在.csv中它没有从re.sub行重命名为Z:\。

3 个答案:

答案 0 :(得分:1)

似乎问题出现在代码底部的循环中。字符串的replace方法不接收列表作为其第一个参数,而是另一个字符串。您需要遍历removeDrives列表并使用该列表中的每个项目调用line.remove

答案 1 :(得分:1)

我可以看到你使用了一些pythonic片段,巧妙地使用了path.join和一个注释代码。这可以变得更好,让我们重写一些事情,这样我们就可以解决你的驱动器号问题,并在途中获得更多的pythonic代码:

#!/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, ))

答案 2 :(得分: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()