用于将文本文件转换为Excel文件的Python程序

时间:2018-02-01 08:55:45

标签: python excel text-files

我有一个文本文件:

Created 30/01/2018 18:28 by Windographer 4.0.26

Latitude = N 9.601639
Longitude = E 98.476861
Elevation = 0 m
Calm threshold = 0 m/s

Included flags: <Unflagged data>
Excluded flags: Invalid

Time stamps indicate the beginning of the time step.

Date/Time   Ban Kong Niam [m/s] 
2008-06-01 00:00    9999
2008-06-01 01:00    9999
2008-06-01 02:00    9999
2008-06-01 03:00    9999
2008-06-01 04:00    9999
2008-06-01 05:00    9999
2008-06-01 06:00    9999
2008-06-01 07:00    9999
2008-06-01 08:00    9999

现在,我想将此文本文件转换为Excel文件。

如果文本文件只包含以下行,我可以编写一个用于创建excel文件的python程序:

Date/Time   Ban Kong Niam [m/s] 
2008-06-01 00:00    9999
2008-06-01 01:00    9999
2008-06-01 02:00    9999
2008-06-01 03:00    9999
2008-06-01 04:00    9999
2008-06-01 05:00    9999
2008-06-01 06:00    9999
2008-06-01 07:00    9999
2008-06-01 08:00    9999

为此,我使用了python代码:

data = []
with open("E:/Sreeraj/Thailand/1. Ban Kong Niam.txt") as f:
for line in f:
    data.append([word for word in line.split("\t") if word])
print data
import xlwt
wb = xlwt.Workbook()
sheet = wb.add_sheet("New Sheet")
for row_index in range(len(data)):
    for col_index in range(len(data[row_index])):
        sheet.write(row_index, col_index, data[row_index][col_index])
wb.save("E:/Sreeraj/Thailand/hi.xls")

这个python代码完美无缺。

但是,我想将整个文本文件转换为Excel文件。因为,我想打印:

Created 30/01/2018 18:28 by Windographer 4.0.26

Latitude = N 9.601639
Longitude = E 98.476861
Elevation = 0 m
Calm threshold = 0 m/s

Included flags: <Unflagged data>
Excluded flags: Invalid

Time stamps indicate the beginning of the time step.

如何创建一个python程序,将整个文本文件与下面给定的行转换为Excel文件?

Created 30/01/2018 18:28 by Windographer 4.0.26

Latitude = N 9.601639
Longitude = E 98.476861
Elevation = 0 m
Calm threshold = 0 m/s

Included flags: <Unflagged data>
Excluded flags: Invalid

Time stamps indicate the beginning of the time step.

Date/Time   Ban Kong Niam [m/s] 
2008-06-01 00:00    9999
2008-06-01 01:00    9999
2008-06-01 02:00    9999
2008-06-01 03:00    9999
2008-06-01 04:00    9999
2008-06-01 05:00    9999
2008-06-01 06:00    9999
2008-06-01 07:00    9999
2008-06-01 08:00    9999

3 个答案:

答案 0 :(得分:1)

您可以使用pandas的IO-Tools获取表格信息:

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_table.html#pandas.read_table

import pandas as pd
df = pd.read_table("filename.txt", header=12, parse_dates=True) # header: row-number of your header
df.to_excel('path_to_file.xlsx', sheet_name='Tabular')

答案 1 :(得分:0)

Excel文件接受.CSV,因此您可以在IDE中打开文本文件,并根据新行在文件中写下您想要的内容。在每个值(标题或数据片段)之间添加逗号&#39;,&#39;。完成文件编写后,将其复制并粘贴到您喜欢的任何位置,然后将其更改为&#34; .CSV&#34;然后双击它,你很高兴

答案 2 :(得分:0)

我创建了一个python程序,它将成功完成此任务。

Created 30/01/2018 18:28 by Windographer 4.0.26

Latitude = N 9.601639
Longitude = E 98.476861
Elevation = 0 m
Calm threshold = 0 m/s

Included flags: <Unflagged data>
Excluded flags: Invalid

Time stamps indicate the beginning of the time step.

Date/Time   Ban Kong Niam [m/s] 
2008-06-01 00:00    9999
2008-06-01 01:00    9999
2008-06-01 02:00    9999
2008-06-01 03:00    9999
2008-06-01 04:00    9999
2008-06-01 05:00    9999
2008-06-01 06:00    9999
2008-06-01 07:00    9999
2008-06-01 08:00    9999

因此,使用我编写的上述python程序,我可以完美地获得excel文件输出,如下所示。

window.requestAnimationFrame(function()
{
    do_before();
    window.requestAnimationFrame(do_after);
});