读取多个txt文件,然后将每个文件另存为具有相同标题的xlsx文件

时间:2018-09-27 13:09:20

标签: python python-3.x pandas

我的任务是使用在文本文件中提供给我的SAP自定义报告-以下示例(删除名称,并使该示例的所有值均为0.00)。任务是使用此数据并根据此数据创建xlsx文件。我在一个特定的文件夹中有100个这些txt文件。我如何将其加载到python中并创建一个xlsx文件,以便将标头数据以相同的位置/格式保存在表格上方的每个文件中?

我有下面的代码为每个文件创建xlsx,但是格式都弄乱了。对于每个单元格/列/行,我都需要更好的格式。

非常感谢您的帮助!

当前代码:

import glob
import os
from xlsxwriter import Workbook
filepath = 'mypath'
txtfiles = glob.glob(os.path.join(filepath, '*z*.txt*'))

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

for filename in txtfiles:
    readfiles = open(filename, 'r') 

    row_list = []

    for row in readfiles:
        row_list.append(row.split('\n'))

    column_list = zip(*row_list)

    workbook = Workbook(filename.replace('.txt', '.xlsx'))
    worksheet = workbook.add_worksheet('Sheet1')

    i = 0
    for column in column_list:
        for item in range(len(column)):
            value = column[item].strip()
            if is_number(value):
                worksheet.write(item, i, float(value))
            else:
                worksheet.write(item, i, value)
        i += 1
    workbook.close()

下面的示例报告:

                                                 SAMPLE REPORT TEMPLATE

Page Number:  1 of   1                                                                         Time of Output:06:37:00
Author of Report:ME                                                                            Date of Output:09/27/2018
Ledger:SAMPLE                                                                                  Version: 1
Currency: USD                                                                                  Fiscal Year:2018
Report Group:RANDOM                                                                            Period:  0 to   10

|.                                                           |    Outside MONEY    | Outside MONEY2      |    Outside MONEY3   |   Subtotal MONIES   |
|------------------------------------------------------------|---------------------|---------------------|---------------------|---------------------|
|   INCOME MONIES BEFORE CERTAIN CALCULATIONS SAMPLE         |                0.00 |                0.00 |                0.00 |                0.00 |
|------------------------------------------------------------|---------------------|---------------------|---------------------|---------------------|
|   1 - Line Data 1                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   1 - Line Data 2                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   1 - Line Data 3                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   1 - Line Data 4                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   1 - Line Data 5                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   2 - Line Data 1                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   2 - Line Data 2                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   2 - Line Data 3                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|*  Sample Random Line W/ Star                               |                0.00 |                0.00 |                0.00 |                0.00 |
|   3 - Line Data 1                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   3 - Line Data 2                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   3 - Line Data 3                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   3 - Line Data 4                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   3 - Line Data 5                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   3 - Line Data 6                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   3 - Line Data 7                                          |                0.00 |                0.00 |                0.00 |                0.00 |
|   3 - Line Data 8                                          |                0.00 |                0.00 |                0.00 |                0.00 |

2 个答案:

答案 0 :(得分:1)

从文本文件读取并将每一行作为一项添加到列表中的操作可以完成以下操作:

lines = []
with open('input_file.txt', 'r') as textinputfile:
    for readline in textinputfile:
        lines.append(readline)

答案 1 :(得分:1)

由于文本报告字段的长度是固定的,因此可以使用一个函数提取标题,而使用另一个函数提取数据。因此,在示例中,每行数据为151个字符。由于标题要短得多,因此您可以轻松地知道当前循环中的行是来自标题还是数据。

我建议将行标题(如果是标题)和“ |”分隔如果是数据。在循环结束时,您将使每一行的字段正确分隔。

如果您在管理数据方面遇到更多麻烦,或者需要更多详细信息,建议使用regex模块。

相关问题