所以我正在运行一个脚本,它为 60 个不同的项目提取数据,为了方便使用,我想将此数据存储在一个 csv 文件中。每次运行脚本时,我都想存储所有 60 个项目,然后覆盖所有以前的数据。当我现在使用 'w' 参数运行它时,它只添加它提取的最后一个项目。如果我将其更改为“a”,它将添加所有 60 个项目,但是当我接下来运行它时它不会覆盖。任何帮助表示赞赏!
import os, random, csv
vin = '1600'
n = 1
for path, subdirs, files in os.walk(r'\\sorion-app01\Shares\ProcessData\Broadcasts\Good'):
for filename in files:
if not 'XX' or not '0X' or not 'XXXXXX' or not '000X' in filename:
f = os.path.join(path, filename)
vins = str(f)[50:67]
if not 'X' in vins[13:17]:
vin = int(vins[13:17])
if vin > 1600:
from sys import platform
import pyodbc
if platform == "linux" or platform == "linux2":
print("linux")
driver = 'mydriver.so'
conn = pyodbc.connect(
driver = driver,
TDS_Version = '7.2', # Use for
server = 'aserver',
port = 1433,
database = 'TraceDB',
uid = 'EXTUser',
pwd = 'EXTPass!')
elif platform == "win32":
#print("Win32")
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=database')
#pull the data here#
with open('file.csv', 'w', newline='') as csvfile:
filewriter = csv.writer(csvfile, delimiter=',',
quotechar='|', quoting=csv.QUOTE_MINIMAL, lineterminator = '\n',)
header = [('column1'), ('column1'), ('column2'), ('column3'), ('column4'), ('column5')]
if not csvfile:
filewriter.writeheader(header)
filewriter.writerow([data, data2, data3, data4, data5, data6])
n=n+1
绝地无双
临摹微笑
相关分类