gpt4 book ai didi

从目录在 SQL 中以 Pythonic 方式上传 CSV 文件,路径错误

转载 作者:行者123 更新时间:2023-11-30 23:26:26 25 4
gpt4 key购买 nike

目前,我的脚本将 .txt 文件从 FTP 站点下载到本地目录(在检查目录是否已包含该文件后),然后将每个文件转换为(单独的).csv。我需要将数据移植到 mysql 数据库中。

这是我遇到问题的部分:运行脚本的第二部分 (ballotreader),当路径不是动态的(即 R:\path\filename)时,文件中的行被导入到 SQL 中,脚本有效并且填充了 sql 表。当路径是动态的时,(即 'R:\path\'+filename)什么都不会填充。有什么建议么?

for filename in filenames:
local_filename = os.path.join('R:\\path', filename)
if os.path.isfile(local_filename) is False:
print 'New file found.'
file = open(local_filename, 'wb')
ftp.retrbinary("RETR " + filename, file.write, 8*1024)
file.close()
print 'Downloaded '+filename+' file'
txt_file = r""+filename
csv_file = r""+filename+".csv"
in_txt = csv.reader(open(txt_file, "rb" ),delimiter = '|')
outcsv = csv.writer(open(csv_file,'wb'))
outcsv.writerows(in_txt)

with open("R:\\path"+csv_file,'rb') as csv_input:
ballotreader = csv.reader(csv_input, delimiter=',',quotechar ='|')
for row in ballotreader:
cursor = db.cursor()
if row[1] > 0:

最佳答案

问题可能出在您构造路径的方式上。尝试使用 os.path.join:

import os

path = os.path.join("R:\\path",csv_file)
with open(path,'rb') as csv_input:
...

关于从目录在 SQL 中以 Pythonic 方式上传 CSV 文件,路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13078507/

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