Method for converting data table plaintext into binary ciphertext convenient for program to read

文档序号:700993 发布日期:2021-04-13 浏览:2次 中文

阅读说明:本技术 数据表格明文转换为便于程序可读取的二进制密文的方法 (Method for converting data table plaintext into binary ciphertext convenient for program to read ) 是由 赵晓丽 于 2020-08-28 设计创作,主要内容包括:本发明公开了数据表格明文转换为便于程序可读取的二进制密文的方法,该发明提供的方法需要和Google提供的开源的Protobuf(版本3.11.x)结合,基于跨平台的编程语言Python。Protobuf是Google提供的一种开源的数据定义格式,使用Protobuf可以将数据表的每个列映射到Protobuf中定义的数据结构中的指定字段,程序中读取转换后的二进制文件,将其转换为对应Protobuf的数据结构类型的数组,对该数组操作,可以进行主键、最大值、最小值等数据表中隐含的数据进行封装操作,也无需再通过读取Excel文件按行号和列号进行数据访问,直接可以通过映射的数据结构的成员进行访问,提高了代码可读性和开发效率。(The invention discloses a method for converting a data table plaintext into a binary ciphertext convenient for program reading, which is combined with open source Protobuf (version 3.11.x) provided by Google and is based on a cross-platform programming language Python. Protobuf is an open-source data definition format provided by Google, each column of a data table can be mapped to a specified field in a data structure defined in Protobuf by using the Protobuf, a converted binary file is read in a program and converted into an array corresponding to the data structure type of the Protobuf, and the array can be operated, so that the data implicit in the data table such as a main key, a maximum value, a minimum value and the like can be packaged, and data access can be directly performed through reading an Excel file according to a row number and a column number, so that the code readability and the development efficiency are improved.)

1. A method for converting a plaintext of a data table into a binary ciphertext that is readable by a program, comprising the steps of:

1) the data table is transmitted into a Python program, the Class of the corresponding data interface is found through the name of the data table, the content of the data table is read into the program through xlrd, and a null array List is declared and used for storing an object converted from the data table into the Class;

2) traversing the content of the data table, if the data table has no content, jumping to step 7), if the data table has content, traversing from the third row of the data table, wherein the first row is a custom header, and the second row is a mapping name of the data structure defined by the line of data and Protobuf;

3) after data of a row is obtained, declaring an object Ins of a Class, continuously traversing the row of data from the 1 st column to obtain cell information, obtaining a second row of data Field of the current cell in the column, and obtaining the data Value of the cell, wherein the Field is the Field name mapped by the data table and the data structure;

4) if the Field does not contain middle brackets, obtaining the type of the Field in the Ins, and converting the cell data Value into a corresponding type Value emod; assigning the member corresponding to the Field in the Ins as ValueMod, if the Field contains middle brackets, indicating that the cell data is one item of a section of continuous data in the data table and corresponding to the data structure as an array; obtaining the data type of the array corresponding to the Field in the Ins, converting the Value into a Value emodr of a corresponding type, and adding the Value emodr to the tail of the array type member corresponding to the Field in the Ins;

5) after the data traversal of the row is finished, obtaining a complete data mapping object Ins, adding Ins to the tail of the List array, and then continuously traversing the content of the data table according to the rows;

6) after traversing the data table, returning an array List of the obtained Class object for binarization and writing into a file for use;

7) traversing the returned Class object array List;

8) each member of the List is a Class type object, i.e., a Python type object corresponding to Protobuf, which contains a method serializtostring that serializes the object into a binary string. Serializing the object into a binary character string BinStr to obtain the length Len of the BinStr, writing the Len into a file, wherein the length of the Len is 4 bytes, and continuously writing the BinStr into the file;

9) and after the traversal of the List is completed, the data table is converted into the ciphertext and then written into the binary file, and the program is ended.

2. The method of claim 1, wherein the contents of the data table are started from a third row, and the data in the second row is a field name of the mapping between the columns of the data table and the data structure.

3. The method of claim 1, wherein the definition name of the data structure and the definition name of the data table need to be consistent.

4. The method of claim 1, wherein type conversion is required between the type defined in the data structure and the data mapped in the data table during parsing.

5. The method of claim 1, wherein the length of the binary string is written into the file before the binary string is written into the file.

Technical Field

The invention relates to a method for reading data in an Excel data table (xlsx, csv and the like) by a program, in particular to a method for converting a data table plaintext into a binary ciphertext which is convenient for the program to read.

Background

The existing method for reading data in Excel data table (xlsx, csv, etc.) by program is relatively cluttered because different programming languages are different for reading Excel data table.

For example, for the C + + programming language, under the Windows platform, the specified Excel file may be accessed using ODBC, or the specified Excel file may be accessed through a COM object provided by Excel; firstly, creating an ODBC or COM object, opening a file, creating an Excel object, and accessing data by a row number and a column number. Under a non-Windows platform, such as Linux, a third-party library libxls needs to be installed, and the use method is similar to that under the Windows platform. As another example, as for the Java programming language, Windows and non-Windows platforms are not involved because Java is a cross-platform programming language. One common method is to refer to an Apache POI-related class library, realize a method Parser for analyzing specified Excel content, realize a method Reader for reading Excel, then read an Excel file through the Reader, and return a result set.

At present, methods for reading Excel files for different programming languages and different platforms analyze Excel files through corresponding libraries provided by Excel, but access is performed on access cell data through rows and columns; therefore, a large amount of hard codes can be generated due to relative rigid comparison in the program development process, and if the rows and columns of Excel are transformed, the program codes also need to be correspondingly changed; if the data table files are packaged into the corresponding application programs, the data table files exist in a plain text, and certain danger is also brought to the safety of the application programs. Therefore, the existing mainstream method for reading Excel data by using a program is not a very friendly way for a demand side and a development side. Therefore, the present invention provides a method for converting the plaintext of the data table into the binary ciphertext that is easy to be read by the program, so as to solve the problems in the background art.

Disclosure of Invention

The present invention is directed to a method for converting a plaintext of a data table into a binary ciphertext that is easy to read by a program, so as to solve the problems mentioned in the background art.

In order to achieve the purpose, the invention provides the following technical scheme:

a method for converting a plaintext of a data table into a binary ciphertext that is readable by a program, the method comprising the steps of:

1) the data table is transmitted into a Python program, the Class of the corresponding data interface is found through the name of the data table, the content of the data table is read into the program through xlrd, and a null array List is declared and used for storing an object converted from the data table into the Class;

2) traversing the content of the data table, if the data table has no content, jumping to step 7), if the data table has content, starting traversing from the third row of the data table (the first row is a custom header, and the second row is a mapping name of the data structure defined by the column of data and Protobuf);

3) after data of a row is obtained, declaring an object Ins of a Class, continuously traversing the row of data from the 1 st column to obtain cell information, obtaining a second row of data Field of the current cell in the column, and obtaining the data Value of the cell, wherein the Field is the Field name mapped by the data table and the data structure;

4) if the Field does not contain middle brackets, obtaining the type of the Field in the Ins, and converting the cell data Value into a corresponding type Value emod; assigning the member corresponding to the Field in the Ins as ValueMod, if the Field contains middle brackets, indicating that the cell data is one item of a section of continuous data in the data table and corresponding to the data structure as an array; obtaining the data type of the array corresponding to the Field in the Ins, converting the Value into a Value emodr of a corresponding type, and adding the Value emodr to the tail of the array type member corresponding to the Field in the Ins;

5) after the data traversal of the row is finished, obtaining a complete data mapping object Ins, adding Ins to the tail of the List array, and then continuously traversing the content of the data table according to the rows;

6) after traversing the data table, returning an array List of the obtained Class object for binarization and writing into a file for use;

7) traversing the returned Class object array List;

8) each member of the List is an object of Class type (i.e., Python type for Protobuf) that contains a method SerializeToString that serializes the object into a binary string. Serializing the object into a binary character string BinStr to obtain the length Len of the BinStr, writing the Len (the length is 4 bytes) into a file, and continuously writing the BinStr into the file;

9) and after the traversal of the List is completed, the data table is converted into the ciphertext and then written into the binary file, and the program is ended.

As a further scheme of the invention: the contents of the data table need to start with the third row, the data of the second row being the field names of the mapping between the columns of the data table and the data structure.

As a still further scheme of the invention: the definition names of the data structure and the definition names of the data table need to be consistent.

As a still further scheme of the invention: when parsing is performed between the type defined in the data structure and the data mapped in the data table, type conversion is required.

As a still further scheme of the invention: before a binary system of a row of data is converted into a file, the length of the binary system needs to be written into the file before the binary character string.

Compared with the prior art, the invention has the beneficial effects that:

1. when data is read for Excel, data does not need to be accessed through line numbers and column numbers, the change of codes caused by the change of a data table in the development process is reduced, the readability of the codes is improved, and the development efficiency is increased.

2. When the data table is packed by the application program, the data table of the plaintext is changed into the binary format of the ciphertext, and the safety of the application program is improved.

3. When the demander provides the data table, the meaning of each column does not need to be explained in detail, the mapping relation of each column only needs to be explained, and the sequence of the columns of the data table does not need to be defined forcibly; the developer can process the data sheet only by analyzing the converted file, and the members of the operation object can process the data sheet, thereby reducing the communication cost of the demander and the developer.

Drawings

FIG. 1 is a diagram of a client information data table illustrating a method for converting plaintext data into binary data that is easily readable by a program.

FIG. 2 is a flow chart of a method for converting plaintext data form into binary ciphertext that is readable by a program.

Detailed Description

The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.

In the embodiment of the invention, a method for converting a data table plaintext into a binary ciphertext convenient for program reading needs to be combined with an open-source Protobuf (version 3.11.x) provided by Google, based on a cross-platform programming language Python, the Protobuf is an open-source data definition format provided by Google, each column of a data table can be mapped to a specified field in a data structure defined in the Protobuf by using the Protobuf, and a third-party library xlrd (version 1.1.0) provided by Python needs to be used for analyzing the data table of Excel.

Referring to fig. 1, there is an Excel data table with a file name of corporation, xssel, which records information of clients of a company, and a Protobuf is used to define a data structure of the corporation, the name of the defined data structure needs to be consistent with that of the data table, and the data structure definition is generated into a corresponding Python definition through a tool provided by Google.

Proto, data structure definition file of company customer information data sheet:

the data of the data table is recorded from the third row, the first row is a self-defined header, and the second row is a mapping relation between the columns of the data table and the data structure defined by the Protobuf.

Referring to FIG. 2, the data table is converted into a binary ciphertext file format for easy program reading, comprising the steps of:

1) the data table is transmitted into a Python program, the Class of the corresponding data interface is found through the name of the data table, the content of the data table is read into the program through xlrd, and a null array List is declared and used for storing an object converted from the data table into the Class;

2) traversing the content of the data table, if the data table has no content, jumping to step 7), if the data table has content, starting traversing from the third row of the data table (the first row is a custom header, and the second row is a mapping name of the data structure defined by the column of data and Protobuf);

3) after data of a row is obtained, declaring an object Ins of a Class, continuously traversing the row of data from the 1 st column to obtain cell information, obtaining a second row of data Field of the current cell in the column, and obtaining the data Value of the cell, wherein the Field is the Field name mapped by the data table and the data structure;

4) if the Field does not contain middle brackets, obtaining the type of the Field in the Ins, and converting the cell data Value into a corresponding type Value emod; assigning the member corresponding to the Field in the Ins as ValueMod, if the Field contains middle brackets, indicating that the cell data is one item of a section of continuous data in the data table and corresponding to the data structure as an array; obtaining the data type of the array corresponding to the Field in the Ins, converting the Value into a Value emodr of a corresponding type, and adding the Value emodr to the tail of the array type member corresponding to the Field in the Ins;

5) after the data traversal of the row is finished, obtaining a complete data mapping object Ins, adding Ins to the tail of the List array, and then continuously traversing the content of the data table according to the rows;

6) after traversing the data table, returning an array List of the obtained Class object for binarization and writing into a file for use;

7) traversing the returned Class object array List;

8) each member of the List is an object of Class type (i.e., Python type for Protobuf) that contains a method SerializeToString that serializes the object into a binary string. Serializing the object into a binary character string BinStr to obtain the length Len of the BinStr, writing the Len (the length is 4 bytes) into a file, and continuously writing the BinStr into the file;

9) and after the traversal of the List is completed, the data table is converted into the ciphertext and then written into the binary file, and the program is ended.

The method provides a file conversion process through Python, and partial codes of a data table conversion ciphertext binary system are as follows:

the algorithm code for ciphertext binary reading and parsing is as follows:

the converted binary file is read in the program and converted into an array corresponding to the data structure type of the Protobuf, the array can be operated, the data implicit in the data tables such as a main key, a maximum value, a minimum value and the like can be packaged, data access is not required to be carried out by reading the Excel file according to the row number and the column number, the access can be directly carried out through members of the mapped data structure, and the code readability and the development efficiency are improved.

The implementation mode of the invention is as follows:

1. language environment Python, library to install, protobuf (version 3.11.2), xlrd (version 1.1.0);

2. tool protoc for installing Protobuf (version 3.11.2);

3. defining a Protobuf data structure mapped by a data table;

4. generating the type of Python of the data structure through a tool protoc of the Protobuf;

5. executing Python program, introducing protobuf library and xlrd library, and generating class defined by data structure. Transmitting the data table file into a program through parameters, and transmitting the generated binary file path into the program;

6. and executing the program, analyzing the data table and generating a ciphertext binary file.

The above description is only for the preferred embodiment of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art should be considered to be within the technical scope of the present invention, and the technical solutions and the inventive concepts thereof according to the present invention should be equivalent or changed within the scope of the present invention.

13页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:文档配图确定方法、装置、电子设备及存储介质

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!