我的脚本没有使用我得到的所有数据重新格式化我的csv,只是标题\\python 3

2024-06-01 09:15:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我的脚本没有用所有数据重新格式化csv,只是标题。当我运行我的代码时,它只给了我标题,数据丢失了。我不知道为什么,我想让输出csv包含我请求的标题和下面的数据

import csv
with open('motherwell.csv','r') as csv_file:
csv_reader = csv.DictReader(csv_file)

with open('stats.csv', 'w') as new_file:
    fieldnames = ['id','name','product','temperature','density','timestamp','water']

    csv_writer = csv.DictWriter(new_file, fieldnames=fieldnames)

    csv_writer.writeheader()

    for line in csv_reader:
        del line['shortname','state','shape','pos_x','pos_y','datum',',max_work','min_work','offset_bottom','offset_volume','offset_level','roof_density','roof_weight','roof_start','roof_end','shell_insulated','shell_cte','shell_temperature','dimension_a','dimension_b','dimension_c','dimension_d','knee_forth','knee_third','knee_second','knee_first','suction_forth,','suction_third','suction_second','suction_first','rundown_forth','rundown_third','rundown_second','rundown_first','suction_low_forth','suction_low_third','suction_low_second','suction_low_first','rundown_low_forth','rundown_low_third','rundown_low_second','rundown_low_first','sll','slu','svl','suv','swlv','swuv','swll','swul','volume','innage','flow','pressure','maximum_volume',]
        csv_writer.writerow(line)

这是带有数据的csv:也是获取csv的链接 https://www.dropbox.com/sh/qdcl2lrrqj6sttb/AABo_AMGzFqetgRne_7XTPy7a?dl=0

"{'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'}"


Tags: shelldensityoffsetlowfirstsecondtemperaturedimension
2条回答

您需要将open('stats.csv','w')更改为新的_文件:此行,并且只将“w”(write)更改为“a”(append)模型。希望这会有所帮助

您的文件不是正确的csv。从技术上讲,你可以认为它是这样的,因为事实上引用的东西都是用逗号分隔的,但是它都在一条线上,这些“事物”看起来就像序列化的Python DISTS(最初认为这是JSON,但是JSON。Load()失败了,而AST。p>

由于只有一行,并且您不向csv.DictReader传递任何字段名,因此它尝试使用第一行作为标题,因此这些类似json的对象中的每一个都被视为字段名。然后,由于使用了唯一一行,迭代停止,这就是结果文件中没有任何数据的原因

你可以自己发现这一点:

f = open("motherwell.csv", "rb")
reader = csv.DictReader(f)
print(reader.fieldnames)
try:
    row = reader.next()
    print(row)
except StopIteration:
    print("nothing else here, dude")

实际上,一个合适的csv文件看起来像(标题和第一行):

 swlv,shell_cte,maximum_volume,offset_bottom,datum,suction_second,swuv,shell_temperature,shape,swul,knee_second,roof_end,suction_low_third,swll,knee_forth,suction_forth,id,dimension_b,roof_start,dimension_a,temperature,dimension_d,rundown_low_second,water,innage,suction_low_first,state,timestamp,shortname,roof_weight,shell_capacity,svl,product,rundown_forth,dimension_c,knee_first,suv,max_work,offset_volume,rundown_third,pressure,roof_density,rundown_low_forth,suction_first,rundown_second,volume,pos_x,pos_y,name,rundown_low_first,shell_gtemp,density,flow,shell_pressure,shell_molecular,shell_insulated,sll,knee_third,offset_level,suction_low_forth,min_work,suction_third,rundown_low_third,rundown_first,suction_low_second,slu
0,0,2.88901e+11,0,0,0.0,0,0,cylinder,0,30.0,0,0.0,0,100.0,0.0,1,76000,0,22000,0.0,0,0.0,0,0.0,0.0,Y,2017-12-14 09:24:48,A,0,0,0.0,14,0.0,0,0.0,2.88901e+11,21000,0,0.0,0,0,0.0,0.0,0.0,0,72,34,1001A,0.0,0,700,0,0,0,N,0.0,70.0,0,0.0,0,0.0,0.0,0.0,0.0,22000.0
0,0,2.88901e+11,0,0,0.0,0,0,cylinder,0,30.0,0,0.0,0,100.0,0.0,2,76000,0,22000,0,0,0.0,0,0.0,0.0,Y,2017-12-14 09:24:48,B,0,0,0.0,14,0.0,0,0.0,2.88901e+11,21000,0,0.0,0,0,0.0,0.0,0.0,0,62,54,1001B,0.0,0,700,0,0,0,N,0.0,70.0,0,0.0,0,0.0,0.0,0.0,0.0,22000.0

长话短说:这不是一个正确的csv文件,而是一个序列化Python dict的串联,因此您不能指望使用csv解析器正确解析它

相关问题 更多 >