2017-12-05 6 views
0

これはPythonを使用するのが簡単かどうかを調べるために、テキストファイルからデータを抽出する必要があります。私は、次のデータを抽出する必要が テスト: 日: 重要なテスト結果の値: 合計値: 全体の実行時間: ABT: RPT: ファイルがテストを大量に含まれている、上記の情報を各ギャザー。 かなり新しいpythonを使用して任意のヘルプ/ポインタが大変感謝します。 失敗数:Pythonでのデータの抽出と並べ替え

+4

このすべてが、しかし、1回の警告で、Pythonで行うことが可能です - あなたが実際にPythonの – WillardSolutions

+0

を学ばなければならないあなただけのpythonを学ぶために始め多分あなたのテキストファイル形式 – gonczor

+0

をあなたの試みを投稿したりすべきですしかし、これが始まったばかりのために複雑になるかどうかは確かではありませんでした。 – mbieren

答えて

0

詳細を提供していないので、私はいくつかの前提を定めました(コードコメントの中にはいくつかあります)。特に、テキストファイル形式(semicolonで区切られたヘッダ付きのCSV)、Excelファイル形式( 'old' XLSファイル形式)を想定していました。次の主要なサブタスクに

あなたの仕事の休憩:

  • 文字列の解析
  • ライティングExcelファイルIは、仮想環境にパッケージをインストール示唆

を読んだテキストファイル(https://docs.python.org/3/tutorial/venv.htmlを参照)などすべてのプロジェクトのベストプラクティスです。

このコードは完全なコードとは異なり、作業の出発点として使用する必要があります。

#!/usr/bin/env python3 

# Do in command line: pip install xlwt 
import xlwt # Assuming you will deal with 'old' XLS files (MS Excel 95, 97, 2000 etc). If you need Excel 2010+ XLSX files, use xlsxwriter. 
# See here for details: http://www.python-excel.org/ 

delimiter = ';' # Assuming your data in text file are delimited with this delimiter 
data_filename = 'data.txt' # source data filename 
xls_filename = f'{data_filename}.xls' # target excel filename 

# dealing with excel, see here: https://github.com/python-excel/xlwt 
book = xlwt.Workbook() # Create new WorkBook 
sh = book.add_sheet('LogReport') # Add sheet named 'LogReport' to WorkBook 


# Read data from text file line by line 
with open('data.txt') as f: 
    for line_number, line in enumerate(f): 
     # line now contains our string 
     items = line.split(delimiter) # split line on items by specified delimiter 
     print(f'items: {items}') # just for debug to see what we've got 

     for col_number, item in enumerate(items): # iterate on each item 
      print(f'{item}') # just for debug 

      # if you need to expand tokens to a separate variables, see: http://www.python.org/dev/peps/pep-3132/ 
      #test_id, test_date, crit_res_value, total_value, overall_runtime, abt, rpt, *tokens = items 

      sh.write(line_number, col_number, item) # Write text to given cell: column, row 
book.save(xls_filename) # save Excel file with desired name 
関連する問題