【问题标题】:Python program to convert a text file into an Excel file将文本文件转换为 Excel 文件的 Python 程序
【发布时间】:2018-02-01 08:55:44
【问题描述】:

我有一个文本文件:

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

【问题讨论】:

  • 你需要 Python 吗? Excel 可以读取制表符分隔的文本文件。
  • @ason​​gtoruin 是的;我必须编写一个python代码。我已经成功编写了成功完成这项任务的python代码。我已经在 Answers 部分给出了 python 代码。谢谢。

标签: python excel text-files


【解决方案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')

【讨论】:

    【解决方案2】:

    Excel 文件接受 .CSV,因此您可以在 IDE 中打开一个文本文件,并在文件中写入您想要的关于新行的内容。在每个值(标题或数据)之间添加一个逗号','。完成写入文件后,将其复制并粘贴到您喜欢的任何位置,然后将其扩展名更改为“.CSV”,然后双击它就可以了

    【讨论】:

      【解决方案3】:

      我创建了一个可以成功完成这项任务的 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文件输出。

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-11
        • 2016-04-07
        相关资源
        最近更新 更多