How can I adapt my code to make it compatible to Microsoft Excel?

∥☆過路亽.° 提交于 2019-12-20 06:28:52

问题


Problem

I was trying to implement an web API(based on Flask), which would be used to query the database given some specific conditions, reconstruct the data and finally export the result to a .csv file.

Since the amount of data is really really huge, I can not construct the whole dataset and generate the .csv file all at once(e.g. create a DataFrame using pandas and finally call df.to_csv()), because that would cause a slow query and maybe the http connection would end up timeout.

So I create a generator which query the database 500 records per time and yield the result one by one, like:

def __generator(q):
    [...]    # some code here
    while True:
        if records == None:
            break
        records = q[offset:offset+limit] # q means a sqlalchemy query object
        [...]   # omit some reconstruct code

        for record in records:
            yield record

and finally construct a Response object, and send .csv to client side:

return Response(__generate(q), mimetype='text/csv')  # Flask

The generator works well and all data are encoded by 'uft-8', but when I try to open the .csv file using Microsoft Excel, it appears to be messy code.

Measures Already Tried

  • add a BOM header to the export file, doesn't work;

  • using some other encode like 'gb18030', and 'cp936', most of the messy code disappear, some still remained, and some part of the table structure become weird.

My Question Is

How can I make my code compatible to Microsoft Excel? That means at least two conditions should be satisfied:

  • no messy code, well displayed;

  • well structured table;

I would be really appreciated for your answer!


回答1:


  • How are you importing the csv file to excel? Have you tried importing the csv as a text file?

By reading as text format for each column, it wont modify columns that it reads as different types like dates. Your code may be correct, and excel may just be modifying the data when it parses it as a csv - by importing as text format, it wont modify anything.




回答2:


I would recommend you look into xlutils. It's been around for quite some time, and our company has used it both for reading configuration files to run automated test and for generating reports of test results.



来源:https://stackoverflow.com/questions/28070098/how-can-i-adapt-my-code-to-make-it-compatible-to-microsoft-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!