[英]Adding timestamp index column with a column header to existing csv (python)
#gets rid of spaces in existing csv headers
def getColumns(readCSV):
return [column.replace(' ','') for column in next(readCSV)]
#insert format used to avoid hard-coded headers in script
def insertData(tableName, columns, readCSV):
print("Inserting Data")
query = 'INSERT INTO {}({}) VALUES ({})'.format(
tableName,
','.join(columns),
','.join('?' * len(columns))
)
for data in readCSV:
cursor.execute(query, data)
con.commit()
def updateTable(csvPath, tableName):
print("Updating table...")
print("Reading file contents and uploading into db table...")
## insert timestamp column into existing csv. Does not incorporate header correctly for timestamp
rows = []
with open(csvPath, 'r', newline='') as csvFile:
readCSV = csv.reader(csvFile, delimiter=',')
for row in readCSV:
rows.append(row)
with open(csvPath, 'w', newline='')as writeFile:
file_write = csv.writer(writeFile)
for val in rows:
timestamp = datetime.now()
val.insert(0, timestamp)
file_write.writerow(val)
with open(csvPath) as csvFile:
readCSV = csv.reader(csvFile, delimiter=',')
columns = getColumns(readCSV)
insertData(tableName, columns, readCSV)
print ("Upload complete")
Above is a snippet of the code I'm working on.以上是我正在处理的代码片段。 I am gathering data from a csv to insert into a SQL database.我正在从 csv 收集数据以插入 SQL 数据库。 Currently, the csv does not have a timestamp column and without that, the import wont work as it sees duplicate data.目前,csv 没有时间戳列,如果没有时间戳列,导入将无法工作,因为它会看到重复的数据。 I found a solution at https://www.geeksforgeeks.org/how-to-add-timestamp-to-csv-file-in-python/ for adding a timestamp column and have incorporated it into the code, but it does not add a header for the column.我在https://www.geeksforgeeks.org/how-to-add-timestamp-to-csv-file-in-python/找到了一个解决方案,用于添加时间戳列并将其合并到代码中,但它没有为列添加标题。 I'm sure it's an easy fix, but I am new to python and cant find the solution anywhere else.我确信这是一个简单的修复,但我是 python 新手,无法在其他任何地方找到解决方案。 Also, if you see something inefficient with the coding in updateTable, let me know so that I can recognize it and learn a better way.另外,如果您发现 updateTable 中的编码效率低下,请告诉我,以便我能够识别它并学习更好的方法。
I ended up using a different method with import pandas as pd
我最终使用了一种不同的方法, import pandas as pd
def updateTable(csvPath, tableName):
print("Updating table...")
print("Reading file contents and uploading into db table...")
timestamp = datetime.now()
df = pd.read_csv(csvPath)
df.insert(0, 'timestamp',timestamp)
df.to_csv(csvPath, index=False)
with open(csvPath) as csvFile:
readCSV = csv.reader(csvFile, delimiter=',')
columns = getColumns(readCSV)
insertData(tableName, columns, readCSV)
print ("Upload complete")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.