如何使用混合數據類型將浮點數格式化爲2個小數點? 我正在讀取表格並將行寫入csv文件。 我的數據是(字符串,字符串,浮球,浮球,浮球......)Python使用字符串和浮點數格式化CSV並寫入
sql = 'select * from testTable'
c.execute(sql)
columnNames = list(map(lambda x: x[0], c.description))
result = c.fetchall()
with open(outputCSVPath, 'wb') as f:
writer = csv.writer(f)
writer.writerow(columnNames)
writer.writerows(result)
與上面的代碼,我與6位小數浮動。我需要將其格式化爲2位小數,但由於列表的前2位是字符串,因此會給我一個類型錯誤。
謝謝。