gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:18:47 25 4
gpt4 key购买 nike

这是代码示例。基本上,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: 在 .csv 中,它没有从 re.sub 行重命名为 Z:\。

最佳答案

我可以看到你使用了一些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, ))

关于Python - 从 csv 文件中删除所有驱动器号并替换为 Z :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1783994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com