无法将数据写入数据文件的CSV文件中的换行符

时间:2019-02-15 07:05:15

标签: python csv dataframe flask-restplus

伙计们,我有一个数据,该数据在datadrame中我想将该数据转储到csv文件中,但我将所有数据都存储在一行中,但我没有如何实现它。请伙计们,它是请求不要使其重复,请在我做错的地方回答这是我的数据:

Asset_Id   Asset Family     Asset Name  Location      Asset Component
3          Nano Dial Assem  NDA11       Zenoa         Fusion Tank                   
1          Haptic Analy HAL3Technopolis Rotation Chamber  Rotation Chamber      
2          Hyperdome Insp   HISP2       Zenoa         Turbo Quantifier  
4          Geometric Synth  GeoSyn25    La Puente     Ion Gas Diffuser              
1          Haptic Analy HAL1Zenoa       Tetris Measuring  Unit  Measurement             
2          Hyperdome Insp   HISP1       Technopolis   Laser Column              
3          Nano Dial Assem  NDA14       Zenoa         Wave Generator                    
4          Geometric Synth  GeoSyn24    La Puente     Progeometric Plane    

“数据”包含我上面显示的值。这是我写的行,请编辑相同的代码

 data = dataGen.compute(uploaded_file, recordCount)
     #Output the generated data to the browser
     proxy = io.StringIO()
     writer = csv.writer(proxy)
     writer.writerow(data.to_records(index=False))

     # Creating the byteIO object from the StringIO Object
     mem = io.BytesIO()
     mem.write(proxy.getvalue().encode('utf-8'))
      # mem.write(proxy.getvalue())

      # seeking was necessary. Python 3.5.2, Flask 0.12.2
     mem.seek(0)

     return send_file(
             mem,
             as_attachment=True,
             attachment_filename='test.csv',
             mimetype='text/csv'
                )    

请帮助我如何将这些数据放入换行符,因为我将所有数据都放在一行中,或者建议进行任何编辑,请告诉我。...thnx

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试将CS​​V文件写入内存,然后将send_file()与内存版本一起使用。您应该可以使用df.to_csv()直接以CSV格式写入数据,如下所示:

df = dataGen.compute(uploaded_file, recordCount)

mem = io.StringIO()
df.to_csv(mem, index=False, encoding='utf-8')
mem.seek(0)

return send_file(
    mem,
    as_attachment=True,
    attachment_filename='test.csv',
    mimetype='text/csv'
)