简体   繁体   English

将数据导出为 xls 文件格式

[英]export data to xls file format

I have a text file with data of 6000 records in this format我有一个包含 6000 条记录数据的文本文件

{"id":"1001","user":"AB1001","first_name":"David  ","name":"Shai","amount":"100","email":"me@no.mail","phone":"9999444"}

{"id":"1002","user":"AB1002","first_name":"jone  ","name":"Miraai","amount":"500","email":"some1@no.mail","phone":"98894004"}

I want to export all data to excel file as shown bellow example我想将所有数据导出到 excel 文件,如下例所示

在此处输入图片说明

I would recommend reading in the text file, then converting to a dictionary with json , and using pandas to save a .csv file that can be opened with excel.我建议阅读文本文件,然后使用json转换为字典,并使用pandas保存可以用 excel 打开的 .csv 文件。

In the example below, I copied your text into a text file, called "myfile.txt", and I saved the data as "myfile2.csv".在下面的示例中,我将您的文本复制到一个名为“myfile.txt”的文本文件中,并将数据保存为“myfile2.csv”。

import pandas as pd
import json 

# read lines of text file
with open('myfile.txt') as f:
    lines=f.readlines()

# remove empty lines
lines2 = [line for line in lines if not(line == "\n")]

# convert to dictionaries
dicts = [json.loads(line) for line in lines2]  

# save to .csv
pd.DataFrame(dicts ).to_csv("myfile2.csv", index = False)

You can use VBA and a json-parser您可以使用 VBA 和 json-parser

Your two lines are not a valid JSON.您的两行不是有效的 JSON。 However, it is easy to convert it to a valid JSON as shown in the code below.但是,很容易将其转换为有效的 JSON,如下面的代码所示。 Then it is a relatively simple matter to parse it and write it to a worksheet.那么解析它并将其写入工作表是一件相对简单的事情。

The code assumes no blank lines in your text file, but it is easy to fix if that is not the case.该代码假定您的文本文件中没有空行,但如果不是这种情况,则很容易修复。

Using your data on two separate lines in a windows text file (if not windows, you may have to change the replacement of the newline token with a comma depending on what the generating system uses for newline.在 windows 文本文件中的两个单独行上使用您的数据(如果不是 windows,您可能必须根据生成系统用于换行的内容,用逗号更改换行标记的替换。

I used the JSON Converter by Tim Hall我使用了Tim HallJSON Converter

'Set reference to Microsoft Scripting Runtime or
'    use late binding
Option Explicit
Sub parseData()
    Dim JSON As Object
    Dim strJSON As String
    Dim FSO As FileSystemObject, TS As TextStream
    Dim I As Long, J As Long

    Dim vRes As Variant, v As Variant, O As Object
    Dim wsRes As Worksheet, rRes As Range

Set FSO = New FileSystemObject
Set TS = FSO.OpenTextFile("D:\Users\Ron\Desktop\New Text Document.txt", ForReading, False, TristateUseDefault)

'Convert to valid JSON
strJSON = "[" & TS.ReadAll & "]"
strJSON = Replace(strJSON, vbLf, ",")

Set JSON = parsejson(strJSON)

ReDim vRes(0 To JSON.Count, 1 To JSON(1).Count)

'Header row
J = 0
For Each v In JSON(1).Keys
    J = J + 1
    vRes(0, J) = v
Next v

'populate the data
I = 0
For Each O In JSON
    I = I + 1
    J = 0
    For Each v In O.Keys
        J = J + 1
        vRes(I, J) = O(v)
    Next v
Next O

'write to a worksheet
Set wsRes = Worksheets("sheet6")
Set rRes = wsRes.Cells(1, 1)

Set rRes = rRes.Resize(UBound(vRes, 1) + 1, UBound(vRes, 2))

Application.ScreenUpdating = False
With rRes
    .EntireColumn.Clear
    .Value = vRes
    .Style = "Output"
    .EntireColumn.AutoFit
End With

End Sub

Results from your posted data您发布的数据的结果

在此处输入图片说明

Try using the pandas module in conjunction with the eval() function:尝试将pandas模块与eval()函数结合使用:

import pandas as pd
with open('textfile.txt', 'r') as f:
    data = f.readlines()

df = pd.DataFrame(data=[eval(i) for i in data])
df.to_excel('filename.xlsx', index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM