使用Python拆分CSV文件不会显示Excel中的所有数据 [英] Split CSV file using Python shows not all data in Excel

查看:235
本文介绍了使用Python拆分CSV文件不会显示Excel中的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的Django数据库中的值转储到csv,然后将csv的内容写入Excel电子表格,看起来像一个表(每个单元格一个值),以便我的用户可以导出电子表格所有数据库中的记录都来自Django admin。现在当我导出文件,我得到这个(只有一个随机值,许多,并且格式不正确):



< a>



我做错了什么?不确定我是否使用列表推导错误,读取文件不正确,或者如果 for 循环有错误。请帮忙!

  def dump_table_to_csv(db_table,io):
with connection.cursor()as cursor:
cursor .execute(SELECT * FROM%s%db_table,[])
row = cursor.fetchall()
writer = csv.writer(io)
writer.writerow([i [ 0] for i in cursor.description])
writer.writerow(row)

with open('/ Users / nicoletorek / emarshal / myfile.csv','w')as f :
dump_table_to_csv(Attorney._meta.db_table,f)

with open('/ Users / nicoletorek / emarshal / myfile.csv','r')as f:
db_list = f.read()
split_db_list = db_list.split(',')

output = BytesIO()
workbook = xlsxwriter.Workbook worksheet_s = workbook.add_worksheet(Summary)

header = workbook.add_format({
'bg_color':'#F7F7F7',
'color':'black'
'align':'center',
'valign':'top',
'border':1
})

row = 0
col = 0

for x in split_db_list:
worksheet_s.write(row + 1,col + 1,x,header)


解决方案

Jean-Francois指出,示例代码的直接问题是,在循环中递增计数器。此外,您还可以使用 xlsxwriter.write_row()而不是 xlsxwriter.write()更可读。



如果您的资料如下所示:

如果您的资料如下所示:

  row_data = [[r1c1,r1c2],[r2c1,r2c2],...] 



您可以使用:

  enumerate(row_data)中的行:
worksheet_s.write_row(index,0,row)

也就是说,我假设你对.xlsx感兴趣,因为你想控制格式化。如果目标是仅生成.xlsx并且没有中间.csv的需要,为什么不直接创建.xlsx文件?这可以很好地在一个视图中实现:

  import io 
从django.http import HttpResponse

def dump_attorneys_to_xlsx(request):

output = io.BytesIO()
workbook = xlsxwriter.Workbook(output,{'in_memory':True})
worksheet = workbook.add_worksheet('Summary')

attorneys = Attorney.objects.all()。values()

写入标题
worksheet.write_row(0, 0,attorneys [0] .keys())

#为row_index,row_dict在enumerate(attorneys,start = 1)中写数据

worksheet.write_row(row_index, 0,row_dict.values())
workbook.close()

output.seek(0)

response = HttpResponse(output.read(),content_type ='application / vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response ['Content-Disposition'] ='attach; filename = summary.xlsx'

返回响应


I am trying to dump the values in my Django database to a csv, then write the contents of the csv to an Excel spreadsheet which looks like a table (one value per cell), so that my users can export a spreadsheet of all records in the database from Django admin. Right now when I export the file, I get this (only one random value out of many and not formatted correctly):

What am I doing wrong? Not sure if I am using list comprehensions wrong, reading the file incorrectly, or if there is something wrong with my for loop. Please help!

def dump_table_to_csv(db_table, io):
    with connection.cursor() as cursor:
        cursor.execute("SELECT * FROM %s" % db_table, [])
        row = cursor.fetchall()
        writer = csv.writer(io)
        writer.writerow([i[0] for i in cursor.description])
        writer.writerow(row)

    with open('/Users/nicoletorek/emarshal/myfile.csv', 'w') as f:
        dump_table_to_csv(Attorney._meta.db_table, f)

with open('/Users/nicoletorek/emarshal/myfile.csv', 'r') as f:
    db_list = f.read()
    split_db_list = db_list.split(',')

    output = BytesIO()
    workbook = xlsxwriter.Workbook(output)
    worksheet_s = workbook.add_worksheet("Summary")

    header = workbook.add_format({
        'bg_color': '#F7F7F7',
        'color': 'black',
        'align': 'center',
        'valign': 'top',
        'border': 1
    })

    row = 0
    col = 0

    for x in split_db_list:
        worksheet_s.write(row + 1, col + 1, x, header)

解决方案

The immediate problem with your sample code, as Jean-Francois points out, is that you aren't incrementing your counters in the loop. Also you may also find it more readable to use xlsxwriter.write_row() instead of xlsxwriter.write(). At the moment a secondary complication is you aren't preserving row information when you read in your data from the CSV.

If your data looks like this:

row_data = [[r1c1, r1c2], [r2c1, r2c2], ... ]

You can then use:

for index, row in enumerate(row_data):
    worksheet_s.write_row(index, 0, row)

That said, I assume you are interested in the .xlsx because you want control over formatting. If the goal is to just to generate the .xlsx and there is no need for the intermediate .csv, why not just create the .xlsx file directly? This can be accomplished nicely in a view:

import io
from django.http import HttpResponse

def dump_attorneys_to_xlsx(request):

    output = io.BytesIO()
    workbook = xlsxwriter.Workbook(output, {'in_memory': True})
    worksheet = workbook.add_worksheet('Summary')

    attorneys = Attorney.objects.all().values()

    # Write header
    worksheet.write_row(0, 0, attorneys[0].keys())

    # Write data
    for row_index, row_dict in enumerate(attorneys, start=1):
        worksheet.write_row(row_index, 0, row_dict.values())
    workbook.close()

    output.seek(0)

    response = HttpResponse(output.read(), content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
    response['Content-Disposition'] = 'attachment; filename=summary.xlsx'

    return response

这篇关于使用Python拆分CSV文件不会显示Excel中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆