Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文本文件转换为Excel文件的Python程序_Python_Excel_Text Files - Fatal编程技术网

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

将文本文件转换为Excel文件的Python程序,python,excel,text-files,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

我有一个文本文件:

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代码:

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.
由Windrographer 4.0.26创建于2018年1月30日18:28
纬度=北纬9.601639度
经度=E 98.476861
海拔=0米
平静阈值=0 m/s
包括旗帜:
排除的标志:无效
时间戳表示时间步的开始。
如何制作一个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
由Windrographer 4.0.26创建于2018年1月30日18:28
纬度=北纬9.601639度
经度=E 98.476861
海拔=0米
平静阈值=0 m/s
包括旗帜:
排除的标志:无效
时间戳表示时间步的开始。
日期/时间班岗鸟[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文件接受.CSV,这样您就可以在IDE中打开一个文本文件,并在文件中写入有关新行的内容。在每个值(标题或数据段)之间添加逗号“,”。编写完文件后,将其复制并粘贴到您喜欢的任何位置,然后将其扩展名更改为“.CSV”,然后双击它,您就可以使用Excel文件接受.CSV了,这样您就可以在IDE中打开一个文本文件,并在文件中针对新行写入所需内容。在每个值(标题或数据段)之间添加逗号“,”。编写完文件后,将其复制并粘贴到您喜欢的任何位置,然后将其扩展名更改为“.CSV”,然后双击它,您就可以开始了

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

将熊猫作为pd导入
df=pd.read_table(“filename.txt”,header=12,parse_dates=True)#header:header的行号
df.to_excel('path_to_file.xlsx',sheet_name='Tabular'))

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

将熊猫作为pd导入
df=pd.read_table(“filename.txt”,header=12,parse_dates=True)#header:header的行号
df.to_excel('path_to_file.xlsx',sheet_name='Tabular'))

我创建了一个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)
导入xlrd
从xlwt导入工作簿
数据=[]
头=[]
合并列表=[]
path=“E:/Sreeraj/tailand/”
file=“1.Ban Kong Niam”
text=路径+文件+“.txt”
excel=路径+文件+“.xls”
打开(文本)作为f:
对于cnt,枚举(f)中的行:
如果cnt<12:
mergedlist.append(行)
打开(文本)作为f:
对于f中的行:
data.append([行中的字对字分割(“\t”)如果字])
合并列表=头+数据
导入xlwt
wb=xlwt.Workbook()
工作表=工作表。添加工作表(“新工作表”)
对于范围(len(mergedlist))中的行索引:
对于范围内的列索引(len(合并列表[行索引]):
sheet.write(行索引、列索引、合并列表[行索引][列索引])
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
由Windrographer 4.0.26创建于2018年1月30日18:28
纬度=北纬9.601639度
经度=E 98.476861
海拔=0米
平静阈值=0 m/s
包括旗帜:
排除的标志:无效
时间戳表示时间步的开始。
日期/时间班岗鸟[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程序,它将成功完成这项任务

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)
导入xlrd
从xlwt导入工作簿
数据=[]
头=[]
合并列表=[]
path=“E:/Sreeraj/tailand/”
file=“1.Ban Kong Niam”
text=路径+文件+“.txt”
excel=路径+文件+“.xls”
打开(文本)作为f:
对于cnt,枚举(f)中的行:
如果cnt<12:
mergedlist.append(行)
打开(文本)作为f:
对于f中的行:
data.append([行中的字对字分割(“\t”)如果字])
合并列表=头+数据
导入xlwt
wb=xlwt.Workbook()
工作表=工作表。添加工作表(“新工作表”)
对于范围(len(mergedlist))中的行索引:
对于范围内的列索引(len(合并列表[行索引]):
sheet.write(行索引、列索引、合并列表[行索引][列索引])
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
由Windrographer 4.0.26创建于2018年1月30日18:28
纬度=北纬9.601639度
经度=E 98.476861
海拔=0米
平静阈值=0 m/s
包括旗帜:
排除的标志:无效
时间戳表示时间步的开始。
日期/时间班岗鸟[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可以读取以制表符分隔的文本文件。@asongtoruin是;我必须要写一个python代码。我已经成功地编写了成功完成这项任务的python代码。我已经在答案部分给出了python代码。谢谢。你需要Python吗?Excel可以读取以制表符分隔的文本文件。@asongtoruin是;我必须要写一个python代码。我已经成功地编写了成功完成这项任务的python代码。我已经在答案部分给出了python代码。非常感谢。