所以我现在的问题是读取一个.txt文件并提取特定的数据。然后将这些数据写入一个excel文件中,现在这些行将成为列,反之亦然。到目前为止,我已经能够读取文件并创建带有列的Excel表格,但没有数据传输。下面是它读取的数据以及写入excel的内容。用python解析excel
[' O/F=7.1800E+00 PERCENT FUEL= 12.2249 EQUIVALENCE RATIO=5.5559E-01 DENSITY=9.5028E-01']
[]
[' CHAMBER THROAT EXIT EXIT']
[' PC/P 1.0000 1.7346 1.0083 62.915']
[' P', ' PSIA 1500.0 864.8 1487.7 23.84']
[' T', ' DEG R 5886 5555 5880 3494']
[' H', ' BTU/LB -446.6 -674.1 -450.1 -1819.9']
[' S', ' BTU/(LB)(R) 2.3395 2.3395 2.3395 2.3395']
[' DEN (LBM/FT3) 6.50E-01 4.01E-01 6.45E-01 1.81E-02']
[' ']
[' M', ' MOL WT 27.357 27.616 27.361 28.499']
[' (DLV/DLP)T -1.01645 -1.01277 -1.01639 -1.00034']
[' (DLV/DLT)P 1.3300 1.2733 1.3292 1.0126']
[' CP', 'BTU/(LB)(R) 0.9274 0.8626 0.9265 0.4371']
[' CP GAS(SF) 0.4353 0.4326 0.4353 0.4041']
[' GAMMA GAS(SF) 1.2001 1.1993 1.2001 1.2084']
[' GAMMA (S) 1.1390 1.1395 1.1390 1.1949']
[' SON VEL', 'FT/SEC 3490.4 3375.7 3488.6 2698.9']
[' MU', 'LBF-S/FT2 2.01E-06 1.93E-06 2.01E-06 1.41E-06']
[' K', 'LBF/S-DEGR 3.16E-02 3.01E-02 3.16E-02 2.03E-02']
[' PRANDTL NO 0.69205 0.69371 0.69207 0.70189']
[' MACH NUMBER 0.0000 1.0000 0.1202 3.0726']
[' ']
[' AE/AT 1.0000 4.9993 9.0000']
[' CSTAR', ' FT/SEC 5139 5139 5139']
[' CF VAC 1.233 1.757']
[' CF 0.657 1.614']
[' IVAC', 'LBF-S/LBM 197.00 280.59']
[' I', ' LBF-SEC/LBM 104.92 257.74']
[' MOL WT(MIX) 27.357 27.616 27.361 28.499']
下面是代码我到目前为止
import csv
import sys
import xlsxwriter
import pandas as pd
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='Read POST run outputs.')
parser.add_argument('infile',
type=argparse.FileType('r'),
help='POST file to be analyzed.')
parser.add_argument('outfile',
type=argparse.FileType('w'),
help='Output xlsx file.')
return parser.parse_args()
def read_post_file(LOX_CH4_GG_5zn.txt, LOX_CH4_GG_5zn.csv):
f = open(LOX_CH4_GG_5zn.txt,"r")
csv_f = csv.reader(f)
relevantData = False
firstDataSet = True
for row in csv_f:
string = str(row)
if 'O/F' in string and firstDataSet:
relevantData = True
elif 'MOLE FRACTIONS' in string:
relevantData = False
firstDataSet = False
if firstDataSet and relevantData:
print (string)
f.close()
if __name__ == "__main__":
args = parse_args()
read_post_file(args.infile.name, args.outfile.name)
workbook = xlsxwriter.Workbook('Com.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column('A:A', 20)
bold = workbook.add_format({'bold': True})
worksheet.write('A1', 'O/F', bold)
worksheet.write('B1', 'PC/P', bold)
worksheet.write('C1', 'P,PSIA', bold)
worksheet.write('D1', 'T,DEG(R)', bold)
worksheet.write('E1', 'H,BTU/LB', bold)
worksheet.write('F1', 'S,BTU/(LB)(R)', bold)
worksheet.write('G1', 'DEN,LBM/FT3', bold)
worksheet.write('H1', 'M,MOL WT', bold)
worksheet.write('I1', '(DLV/DLP)T', bold)
worksheet.write('J1', '(DLV/DLP)P', bold)
worksheet.write('I1', 'CP,BTU/(LB)(R)', bold)
worksheet.write('K1', 'CP GAS,SF', bold)
worksheet.write('L1', 'GAMMA GAS,SF', bold)
worksheet.write('K1', 'GAMMA,S', bold)
worksheet.write('K1', 'SON VEL,FT/SEC', bold)
worksheet.write('K1', 'MU,LBF-S/FT2', bold)
worksheet.write('K1', 'K,LBF/S-DEG(R)', bold)
worksheet.write('K1', 'PRANDTL NO', bold)
worksheet.write('K1', 'MACH NUMBER', bold)
worksheet.write('K1', 'AE/AT', bold)
worksheet.write('K1', 'CSTAR,FT/SEC', bold)
worksheet.write('K1', 'CF VAC', bold)
worksheet.write('K1', 'CF', bold)
worksheet.write('K1', 'IVAC, LBF-SEC/LBM', bold)
worksheet.write('K1', 'I,LBF-SEC/LBM', bold)
worksheet.write('K1', 'MOL WT(MIX)', bold)
workbook.close()
任何帮助是极大的赞赏一个这个。
输入1
输出内容是被张贴在问题的开始。
2016-03-07
Z_Russ89
+0
请提供一个样本输入TXT文件(5-7行就足够了)和期望的输出(它可能也是CSV格式或链接到Excel文件) –
+0
为什么K1覆盖很多时间。这是正确的代码吗? –
+0
你应该写信给C:\ Users \ Public .....我也不知道你在写什么。可能没有privs ...并确保你正在查看正确的文件...例如, workbook = xlsxwriter.Workbook('C:\ Users ..... \ Test.xlsx') –