【发布时间】:2019-09-19 15:12:24
【问题描述】:
我在一个文件夹中有 3 个区域特定的 CSV 文件,即 data_cityA.csv、data_cityB.csv 和 data_cityC.csv。我必须阅读并识别特定于区域的文件;将其插入到表中,并添加一个额外的列,该列将包含有关特定区域的信息。
list_of_file=glob.glob('./*csv')
for file_name in list_of_files:
count = 0
total = 0
with open(file_name,'r')as csvfile:
read=csv.reader(csvfile)
next(read)
if "cityA" in file_name:
reg="cityA"
elif "cityB" in file_name:
reg="cityB"
elif "cityC" in file_name:
reg="cityC"
with open(file_name, 'r')as csv_file:
reader=csv.reader(csv_file)
data=list(reader)
total=len(data)
temp_data=[]
for row in read:
row.append(reg) #concatenating region name
temp_data.append(tuple(row))
count+=1
total=-1
if count>999 or total==1:
insert_query="INSERT INTO table_name(A,B,C,D,E) values (1,2,3,4,5)"
curser.executemoany(insert_query,temp_data)
conn.commit()
count=0
insert_query=" "
temp_data=[]
cursor.callproc('any_proc')
conn.close()
处理大约需要 4-5 小时(数据大小 sybase。有任何想法吗?有没有比多处理更好的方法?
【问题讨论】:
标签: python database csv multiprocessing sybase