csv在python_在Python中更新CSV文件

在Python中更新CSV文件时遇到问题,代码尝试读取'datetime.csv'并比较文件修改时间,但出现'iterator should return strings, not bytes'错误。尝试用DictReader和DictWriter操作CSV,如果文件已存在则更新,否则追加新记录。" 111080771,10294818,Python爬虫:爬取喜马拉雅有声书音频,"['Python', '网络爬虫', '数据抓取', '音频处理', '数据存储']
摘要由CSDN通过智能技术生成

我目前正在工作的CSV文件创建编辑和附加到CSV文件的问题是,我正在尝试更新到现有的CSV文件,但遇到了这样做的麻烦。在

当我运行代码时,我得到以下错误。在/usr/bin/python3.5 /home/usr/PycharmProjects/csv/csv_file.py

2016-11-12 00:04:03.975247

2016-11-11 23:59:17.591426

MOD TIME IS Different

Traceback (most recent call last):

File "/home/usr/PycharmProjects/csv/csv_file.py", line 157, in

Direcotry_function(sys.argv)

File "/home/usr/PycharmProjects/csv/csv_file.py", line 133, in Direcotry_function

for row in reader:

File "/usr/lib/python3.5/csv.py", line 109, in __next__

self.fieldnames

File "/usr/lib/python3.5/csv.py", line 96, in fieldnames

self._fieldnames = next(self.reader)

_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

Process finished with exit code 1

下面是我似乎无法运行的Python代码。在

^{pr2}$

编辑:

所以,在玩弄了代码之后:我在做这件事之后,我要面对的问题是没有阅读整个代码日期时间.csv如果我更改了一个文件中的某些内容,我就会得到这个

它不会给我测试温度csv在

文件名,修改日期

/主页/usr/Desktop/test232016-11-12 11:22:07.410170

文件名,修改日期

/主页/usr/Desktop/test232016-11-12 11:22:07.410170

文件名,修改日期

/主页/usr/Desktop/test232016-11-12 11:22:07.410170def Direcotry_function(directory_dest):

global directory_path #call the Global Variable

directory_path = os.getcwd() # getting Current Directory

if len(directory_dest) > 1:

directory_path = directory_dest

for list in filepath_list:

Originalfiles = ''.join(list)#Convert to Str

drive, path = os.path.splitdrive(Originalfiles)#Get the Path

path, filename = os.path.split(path)#Split the path and filename

makedir = path + '/' + filename

Originalfiles_Timestamp = os.path.getmtime(Originalfiles)#Get the Timestamp of File in seconds

mTime = datetime.datetime.fromtimestamp(Originalfiles_Timestamp)#Get the Date time in Month Yearformat

print(mTime)

print(filename)

if os.path.exists(os.path.join(directory_path, filename[:]+".copy")):#Check if the FIle exists

file_csv = csv.DictReader(open('datetime.csv'))#open CSV FILE

for row in file_csv:#Loop over

if row['FileName'] == Originalfiles:

rowTime = datetime.datetime.strptime(row['ModificationDate'], "%Y-%m-%d %H:%M:%S.%f") #Convert row to DateTime

if rowTime == mTime: #Check if Time inside CSV equals mTime

print("same")

else:

with open('datetime.csv', "r") as f:

reader = csv.DictReader(f)

fieldnames = ['FileName', 'ModificationDate'] # Create Dict Keys

for row in reader:

with open("temp.csv", 'w') as g:

writer = csv.DictWriter(g, fieldnames=fieldnames)

writer.writeheader()

writer.writerow({"FileName": Originalfiles, "ModificationDate": mTime})

#shutil.move("temp.csv", 'datetime.csv')

os.system('cp -rv ' + makedir + ' ' + directory_path + '/' + filename[:] + ".copy")

else:

#If Copy File Does not Exists in the Folder do the Following:

os.system('cp -r ' + makedir + ' ' + directory_path + '/' + filename[:]+".copy")#Copy ther File to the Directory

with open('datetime.csv', 'a') as csvfile:#Open CSV File

fieldnames = ['FileName', 'ModificationDate']#Create Dict Keys

writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

writer.writeheader()

writer.writerow({'FileName': Originalfiles, 'ModificationDate': mTime})#Write to The CSV File

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值