python 3我的csv格式不正确

时间:2018-01-10 14:56:56

标签: python csv

我试图将数据从另一个python脚本拉到我的python脚本中,允许下载数据并将数据转换成表格格式的csv但是当我把它拉出来时它会无序地我不知道为什么< / p>

这是用于将数据导入csv的csv puller脚本

import psutil
import sys
import os
import inspect
import socket
from gevent.pywsgi import WSGIServer
from time import gmtime, strftime
from threading import Thread
from flask import Flask, render_template, session, app, request, redirect, 
url_for, send_file, send_from_directory
from flask_cors import CORS, cross_origin
from time import sleep
import requests
import data
import tank
import csv
APP_ROOT = os.path.dirname(os.path.abspath('__file__'))

def funcScript():
    from data import tankOBJ as tank
    for tank in data.tankOBJ:
    printtofile(tank['id'] + ',' + tank['name'] + ',' + tank['density'] + ',' + tank['tempature'] + ',' + tank['product'] + ',' + tank['timestamp'] + ',' + tank['water'])
    with open(os.path.join(os.path.join(APP_ROOT, '/csv tester/motherwell.csv')),'r',newline='') as output_file:
        writer = csv.Dictwriter(output_file, fieldnames=fieldnames, delimiter = '\t')
        csv_writer.writeheader()

        for line in csv_reader:
            writer.writerow(row)
            print ('%s, %s,' %('hello','world'))


return 'Hello World!'

这是我试图提取的数据

tankOBJ = []

"""
Data Entries

id          -   Tank id
name        -   Tank Name
product     -   Product id
state       -   Scan state (Options:Y|N)  
shape       -   Tank Shape
pos_x       -   Map Posistion X
pos_y       -   Map Posistion Y
datum       -   Tank Datum
max_work    -   Tank maximum working level
min_work    -
offset_bottom   -
offset_volume   -
offset_level    -   
roof_density    -
roof_weight     -
roof_start      -
roof_end        -
shell_insulated -   
shell_cte       -
shell_temperature':tank[19],
shell_capacity':tank[20],
shell_molecular':tank[21],
shell_pressure':tank[22],
shell_gtemp':tank[23],
dimension_a':tank[24],
dimension_b':tank[25],
dimension_c':tank[26],
knee_third':tank[28],
knee_second':tank[29],
knee_first':tank[30],
suction_third':tank[31],
suction_second':tank[32],
suction_first':tank[33],
rundown_third':tank[34],
rundown_second':tank[35],
rundown_first':tank[36],
low_third':tank[37],
low_second':tank[38],
low_first':tank[39],
sll':'0',   #Variable Spacers
slu':'1',
svl':'0',
suv':'1',
swlv':'0',
swuv':'1',
swll':'0',
swul':'1',
volume':0,  
level':0,
flow':0,
water':0,
density':700,
pressure':0,
temperature':0,
maximum_volume':strapUpperLimit})

“”“

最后这里是它的输出

"{'id': 1, 'name': '1001A', 'shortname': 'A', 'product': '14', 'state': 'Y', 'shape': 'cylinder', 'pos_x': '72', 'pos_y': '34', 'datum': '0', 'max_work': '21000', 'min_work': '0', 'offset_bottom': '0', 'offset_volume': '0', 'offset_level': '0', 'roof_density': '0', 'roof_weight': '0', 'roof_start': '0', 'roof_end': '0', 'shell_insulated': 'N', 'shell_cte': '0', 'shell_temperature': '0', 'shell_capacity': '0', 'shell_molecular': '0', 'shell_pressure': '0', 'shell_gtemp': '0', 'dimension_a': 22000, 'dimension_b': 76000, 'dimension_c': 0, 'dimension_d': 0, 'knee_forth': 100.0, 'knee_third': 70.0, 'knee_second': 30.0, 'knee_first': 0.0, 'suction_forth': 0.0, 'suction_third': 0.0, 'suction_second': 0.0, 'suction_first': 0.0, 'rundown_forth': 0.0, 'rundown_third': 0.0, 'rundown_second': 0.0, 'rundown_first': 0.0, 'suction_low_forth': 0.0, 'suction_low_third': 0.0, 'suction_low_second': 0.0, 'suction_low_first': 0.0, 'rundown_low_forth': 0.0, 'rundown_low_third': 0.0, 'rundown_low_second': 0.0, 'rundown_low_first': 0.0, 'sll': 0.0, 'slu': 22000.0, 'svl': 0.0, 'suv': 288901000000.0, 'swlv': 0, 'swuv': 0, 'swll': 0, 'swul': 0, 'volume': 0, 'innage': 0.0, 'flow': 0, 'water': 0, 'density': 700, 'pressure': 0, 'temperature': 0.0, 'maximum_volume': 288901000000.0, 'timestamp': '2017-12-14 09:24:48'}","{'id': 2, 'name': '1001B', 'shortname': 'B', 'product': '14', 'state': 'Y', 'shape': 'cylinder', 'pos_x': '62', 'pos_y': '54', 'datum': '0', 'max_work': '21000', 'min_work': '0', 'offset_bottom': '0', 'offset_volume': '0', 'offset_level': '0', 'roof_density': '0', 'roof_weight': '0', 'roof_start': '0', 'roof_end': '0', 'shell_insulated': 'N', 'shell_cte': '0', 'shell_temperature': '0', 'shell_capacity': '0', 'shell_molecular': '0', 'shell_pressure': '0', 'shell_gtemp': '0', 'dimension_a': 22000, 'dimension_b': 76000, 'dimension_c': 0, 'dimension_d': 0, 'knee_forth': 100.0, 'knee_third': 70.0, 'knee_second': 30.0, 'knee_first': 0.0, 'suction_forth': 0.0, 'suction_third': 0.0, 'suction_second': 0.0, 'suction_first': 0.0, 'rundown_forth': 0.0, 'rundown_third': 0.0, 'rundown_second': 0.0, 'rundown_first': 0.0, 'suction_low_forth': 0.0, 'suction_low_third': 0.0, 'suction_low_second': 0.0, 'suction_low_first': 0.0, 'rundown_low_forth': 0.0, 'rundown_low_third': 0.0, 'rundown_low_second': 0.0, 'rundown_low_first': 0.0, 'sll': 0.0, 'slu': 22000.0, 'svl': 0.0, 'suv': 288901000000.0, 'swlv': 0, 'swuv': 0, 'swll': 0, 'swul': 0, 'volume': 0, 'innage': 0.0, 'flow': 0, 'water': 0, 'density': 700, 'pressure': 0, 'temperature': 0, 'maximum_volume': 288901000000.0, 'timestamp': '2017-12-14 09:24:48'}","{'id': 3, 'name': '1001C', 'shortname': 'C', 'product': '14', 'state': 'N', 'shape': 'cylinder', 'pos_x': '80', 'pos_y': '36', 'datum': '0', 'max_work': '21000', 'min_work': '0', 'offset_bottom': '0', 'offset_volume': '0', 'offset_level': '0', 'roof_density': '0', 'roof_weight': '0', 'roof_start': '0', 'roof_end': '0', 'shell_insulated': 'N', 'shell_cte': '0', 'shell_temperature': '0', 'shell_capacity': '0', 'shell_molecular': '0', 'shell_pressure': '0', 'shell_gtemp': '0', 'dimension_a': 22000, 'dimension_b': 76000, 'dimension_c': 0, 'dimension_d': 0, 'knee_forth': 100.0, 'knee_third': 70.0, 'knee_second': 30.0, 'knee_first': 0.0, 'suction_forth': 0.0, 'suction_third': 0.0, 'suction_second': 0.0, 'suction_first': 0.0, 'rundown_forth': 0.0, 'rundown_third': 0.0, 'rundown_second': 0.0, 'rundown_first': 0.0, 'suction_low_forth': 0.0, 'suction_low_third': 0.0, 'suction_low_second': 0.0, 'suction_low_first': 0.0, 'rundown_low_forth': 0.0, 'rundown_low_third': 0.0, 'rundown_low_second': 0.0, 'rundown_low_first': 0.0, 'sll': 0, 'slu': 0, 'svl': 0, 'suv': 0, 'swlv': 0, 'swuv': 0, 'swll': 0, 'swul': 0, 'volume': 0, 'innage': 0, 'flow': 0, 'water': 0, 'density': 700, 'pressure': 0, 'temperature': 0, 'maximum_volume': 288901000000.0, 'timestamp': '2017-12-14 09:24:48'}","{'id': 4, 'name': '1001D', 'shortname': 'D', 'product': '14', 'state': 'N', 'shape': 'cylinder', 'pos_x': '91', 'pos_y': '56', 'datum': '0', 'max_work': '21000', 'min_work': '0', 'offset_bottom': '0', 'offset_volume': '0', 'offset_level': '0', 'roof_density': '0', 'roof_weight': '0', 'roof_start': '0', 'roof_end': '0', 'shell_insulated': 'N', 'shell_cte': '0', 'shell_temperature': '0', 'shell_capacity': '0', 'shell_molecular': '0', 'shell_pressure': '0', 'shell_gtemp': '0', 'dimension_a': 22000, 'dimension_b': 76000, 'dimension_c': 0, 'dimension_d': 0, 'knee_forth': 100.0, 'knee_third': 70.0, 'knee_second': 30.0, 'knee_first': 0.0, 'suction_forth': 0.0, 'suction_third': 0.0, 'suction_second': 0.0, 'suction_first': 0.0, 'rundown_forth': 0.0, 'rundown_third': 0.0, 'rundown_second': 0.0, 'rundown_first': 0.0, 'suction_low_forth': 0.0, 'suction_low_third': 0.0, 'suction_low_second': 0.0, 'suction_low_first': 0.0, 'rundown_low_forth': 0.0, 'rundown_low_third': 0.0, 'rundown_low_second': 0.0, 'rundown_low_first': 0.0, 'sll': 0, 'slu': 0, 'svl': 0, 'suv': 0, 'swlv': 0, 'swuv': 0, 'swll': 0, 'swul': 0, 'volume': 0, 'innage': 0, 'flow': 0, 'water': 0, 'density': 700, 'pressure': 0, 'temperature': 0, 'maximum_volume': 288901000000.0, 'timestamp': '2017-12-14 09:24:48'}","{'id': 5, 'name': '1001E', 'shortname': 'E', 'product': '14', 'state': 'N', 'shape': 'cylinder', 'pos_x': '47', 'pos_y': '24', 'datum': '0', 'max_work': '21000', 'min_work': '0', 'offset_bottom': '0', 'offset_volume': '0', 'offset_level': '0', 'roof_density': '0', 'roof_weight': '0', 'roof_start': '0', 'roof_end': '0', 'shell_insulated': 'N', 'shell_cte': '0', 'shell_temperature': '0', 'shell_capacity': '0', 'shell_molecular': '0', 'shell_pressure': '0', 'shell_gtemp': '0', 'dimension_a': 22000, 'dimension_b': 76000, 'dimension_c': 0, 'dimension_d': 0, 'knee_forth': 100.0, 'knee_third': 70.0, 'knee_second': 30.0, 'knee_first': 0.0, 'suction_forth': 0.0, 'suction_third': 0.0, 'suction_second': 0.0, 'suction_first': 0.0, 'rundown_forth': 0.0, 'rundown_third': 0.0, 'rundown_second': 0.0, 'rundown_first': 0.0, 'suction_low_forth': 0.0, 'suction_low_third': 0.0, 'suction_low_second': 0.0, 'suction_low_first': 0.0, 'rundown_low_forth': 0.0, 'rundown_low_third': 0.0, 'rundown_low_second': 0.0, 'rundown_low_first': 0.0, 'sll': 0, 'slu': 0, 'svl': 0, 'suv': 0, 'swlv': 0, 'swuv': 0, 'swll': 0, 'swul': 0, 'volume': 0, 'innage': 0, 'flow': 0, 'water': 0, 'density': 700, 'pressure': 0, 'temperature': 0, 'maximum_volume': 288901000000.0, 'timestamp': '2017-12-14 09:24:48'}","{'id': 6, 'name': '1001F', 'shortname': 'F', 'product': '14', 'state': 'N', 'shape': 'cylinder', 'pos_x': '62', 'pos_y': '18', 'datum': '0', 'max_work': '21000', 'min_work': '0', 'offset_bottom': '0', 'offset_volume': '0', 'offset_level': '0', 'roof_density': '0', 'roof_weight': '0', 'roof_start': '0', 'roof_end': '0', 'shell_insulated': 'N', 'shell_cte': '0', 'shell_temperature': '0', 'shell_capacity': '0', 'shell_molecular': '0', 'shell_pressure': '0', 'shell_gtemp': '0', 'dimension_a': 22000, 'dimension_b': 76000, 'dimension_c': 0, 'dimension_d': 0, 'knee_forth': 100.0, 'knee_third': 70.0, 'knee_second': 30.0, 'knee_first': 0.0, 'suction_forth': 0.0, 'suction_third': 0.0, 'suction_second': 0.0, 'suction_first': 0.0, 'rundown_forth': 0.0, 'rundown_third': 0.0, 'rundown_second': 0.0, 'rundown_first': 0.0, 'suction_low_forth': 0.0, 'suction_low_third': 0.0, 'suction_low_second': 0.0, 'suction_low_first': 0.0, 'rundown_low_forth': 0.0, 'rundown_low_third': 0.0, 'rundown_low_second': 0.0, 'rundown_low_first': 0.0, 'sll': 0, 'slu': 0, 'svl': 0, 'suv': 0, 'swlv': 0, 'swuv': 0, 'swll': 0, 'swul': 0, 'volume': 0, 'innage': 0, 'flow': 0, 'water': 0, 'density': 700, 'pressure': 0, 'temperature': 0, 'maximum_volume': 288901000000.0, 'timestamp': '2017-12-14 09:24:49'}","{'id': 7, 'name': '1001G', 'shortname': 'G', 'product': '14', 'state': 'N', 'shape': 'cylinder', 'pos_x': '69', 'pos_y': '14', 'datum': '0', 'max_work': '21000', 'min_work': '0', 'offset_bottom': '0', 'offset_volume': '0', 'offset_level': '0', 'roof_density': '0', 'roof_weight': '0', 'roof_start': '0', 'roof_end': '0', 'shell_insulated': 'N', 'shell_cte': '0', 'shell_temperature': '0', 'shell_capacity': '0', 'shell_molecular': '0', 'shell_pressure': '0', 'shell_gtemp': '0', 'dimension_a': 22000, 'dimension_b': 76000, 'dimension_c': 0, 'dimension_d': 0, 'knee_forth': 100.0, 'knee_third': 70.0, 'knee_second': 30.0, 'knee_first': 0.0, 'suction_forth': 0.0, 'suction_third': 0.0, 'suction_second': 0.0, 'suction_first': 0.0, 'rundown_forth': 0.0, 'rundown_third': 0.0, 'rundown_second': 0.0, 'rundown_first': 0.0, 'suction_low_forth': 0.0, 'suction_low_third': 0.0, 'suction_low_second': 0.0, 'suction_low_first': 0.0, 'rundown_low_forth': 0.0, 'rundown_low_third': 0.0, 'rundown_low_second': 0.0, 'rundown_low_first': 0.0, 'sll': 0, 'slu': 0, 'svl': 0, 'suv': 0, 'swlv': 0, 'swuv': 0, 'swll': 0, 'swul': 0, 'volume': 0, 'innage': 0, 'flow': 0, 'water': 0, 'density': 700, 'pressure': 0, 'temperature': 0, 'maximum_volume': 288901000000.0, 'timestamp': '2017-12-14 09:24:49'}","{'id': 8, 'name': '1001H', 'shortname': 'H', 'product': '14', 'state': 'N', 'shape': 'open-floating-roof', 'pos_x': '48', 'pos_y': '54', 'datum': '0', 'max_work': '21000', 'min_work': '0', 'offset_bottom': '0', 'offset_volume': '0', 'offset_level': '0', 'roof_density': '0', 'roof_weight': '0', 'roof_start': '0', 'roof_end': '0', 'shell_insulated': 'N', 'shell_cte': '0', 'shell_temperature': '0', 'shell_capacity': '0', 'shell_molecular': '0', 'shell_pressure': '0', 'shell_gtemp': '0', 'dimension_a': 22000, 'dimension_b': 76000, 'dimension_c': 0, 'dimension_d': 0, 'knee_forth': 100.0, 'knee_third': 70.0, 'knee_second': 30.0, 'knee_first': 0.0, 'suction_forth': 0.0, 'suction_third': 0.0, 'suction_second': 0.0, 'suction_first': 0.0, 'rundown_forth': 0.0, 'rundown_third': 0.0, 'rundown_second': 0.0, 'rundown_first': 0.0, 'suction_low_forth': 0.0, 'suction_low_third': 0.0, 'suction_low_second': 0.0, 'suction_low_first': 0.0, 'rundown_low_forth': 0.0, 'rundown_low_third': 0.0, 'rundown_low_second': 0.0, 'rundown_low_first': 0.0, 'sll': 0, 'slu': 0, 'svl': 0, 'suv': 0, 'swlv': 0, 'swuv': 0, 'swll': 0, 'swul': 0, 'volume': 0, 'innage': 0, 'flow': 0, 'water': 0, 'density': 700, 'pressure': 0, 'temperature': 0, 'maximum_volume': 288901000000.0, 'timestamp': '2017-12-14 09:24:49'}"

我需要它像这样     [id] [name] [密度] [temp] [product] [timestamp] [water]     以及列中的数据

1 个答案:

答案 0 :(得分:0)

查看csv库的文档,我认为你应该传递一个值列表,但是你传递了一个键/值对的字典。

由于您的词典有比csv更多的条目,您必须按顺序指定应使用其值的键:

writer.writerow([row["id"],row["name"]...])