Python程序将文本文件转换为Excel-fi

2024-09-30 08:30:49 发布

您现在位置: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

现在,我想把这个文本文件转换成Excel文件。在

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

^{pr2}$

为此,我使用了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

Tags: 文件theinfordataindexbytime
3条回答

您可以使用pandas的IO工具获取表格信息:

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')

Excel文件接受.CSV文件,因此您可以在IDE中打开一个文本文件,并根据新行在文件中写入所需内容。在每个值(标题或数据段)之间添加逗号“,”。写完文件后,复制它并粘贴到任何你喜欢的地方,然后将它的扩展名改为“.CSV”,然后双击它,你就可以开始了

我创建了一个python程序,它将成功地完成这项任务。在

import xlrd
from xlwt import Workbook

data = []
head = []
mergedlist = []
path = "E:/Sreeraj/Thailand/"
file = "1. Ban Kong Niam"
text = path + file + ".txt"
excel = path + file + ".xls"


with open(text) as f:
    for cnt, line in enumerate(f):
        if cnt < 12:
            mergedlist.append(line)

with open(text) as f:
    for line in f:
        data.append([word for word in line.split("\t") if word])

mergedlist = head + data

import xlwt
wb = xlwt.Workbook()
sheet = wb.add_sheet("New Sheet")
for row_index in range(len(mergedlist)):
    for col_index in range(len(mergedlist[row_index])):
        sheet.write(row_index, col_index, mergedlist[row_index][col_index])


wb.save(excel)

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

^{pr2}$

相关问题 更多 >

    热门问题