2016-05-21 5 views
0

私の販売記録に必要なヘルプ。私のプログラムはテキストファイルの情報を呼び出します。以下 このコードは:私は非値を返す関数のprint_all_records(レコード)を書きませんかプロセスを記録します。 - .txtファイルの情報を呼び出します。 Python

1-2-2014,Frankton,42305.67,23 
12-4-2014,Glenview,21922.22,17 
10-2-2015,Glenview,63277.9,32 
10-5-2015,Glenview,83290.09,16 
11-6-2015,Frankton,81301.82,34 
10-10-2015,Frankton,62333.3,40 
11-11-2015,Frankton,28998.8,29 
1-1-2016,Glenview,51083.5,27 
1-3-2016,Glenview,62155.72,42 
27-3-2016,Frankton,33075.1,18 
2-4-2016,Glenview,61824.7,35 

text_file = open("data.txt", "r") 

lines = text_file.readlines() 

print (lines) 

print (lines) 

text_file.close() 

は、以下の情報が含まれている私のdata.txtをファイルを開きます。関数は、リストを周囲として取り、レコードを表示します。レコードは1行に1つです。異なる見出しの下で

data.txtファイルを参照して例を示します。印刷時にこのように表示されるはずです。各行にはテキストファイルの情報が入っています。

Date   Branch    Daily Sale   Transactions 
01/02/2014 Frankton   $42305.67   23 
12/04/2014 Glenview   $219.22.22   17 
+0

チェックアウトhttps://pypi.python.org/pypi/PrettyTable。あなたが望むフォーマットではありませんが、使用するのはとても簡単ですし、見た目もとてもいいです。 –

+0

この宿題や課題はありますか?ありがとう。 –

答えて

0

あなたが使用することができ、リスト内の文字列として各行を格納している場合:

def print_all_records(records): 
    print("Date" + "\t\t" + "Branch" + "\t\t" + "Daily Sale" + "\t\t" + "Transactions") 
    for record in records: 
     parts = record.split(",") 
     print(parts[0] + "\t" + parts[1] + "\t" + "$" + parts[2] + "\t\t" + parts[3]) 

あなたは明らかにあなた自身の好みに合わせてタブの数(\t)を調整することができます。

0

あなたはパンダを使用することができます -

import pandas as pd 

df = pd.read_csv('file_name.txt', header = None) 

df.columns = ['Date', 'Branch', 'Daily Sale', 'Transactions'] 

print(df.to_string(index = False, justify = 'left')) 

出力 -

Date  Branch  Daily Sale Transactions 
    1-2-2014 Frankton 42305.67 23   
    12-4-2014 Glenview 21922.22 17   
    10-2-2015 Glenview 63277.90 32   
    10-5-2015 Glenview 83290.09 16   
    11-6-2015 Frankton 81301.82 34   
10-10-2015 Frankton 62333.30 40   
11-11-2015 Frankton 28998.80 29   
    1-1-2016 Glenview 51083.50 27   
    1-3-2016 Glenview 62155.72 42   
    27-3-2016 Frankton 33075.10 18   
    2-4-2016 Glenview 61824.70 35 
+0

ありがとうございました。 importerror:pandasという名前のモジュールはありません –

+0

Pandasは外部モジュールです。あなたはそれをインストールする必要があります。 –

+0

ありがとうございました。私はまた、私のプログラムがdata.txtファイルを検索する方法があるのか​​疑問に思っていましたか?たとえば、data.txtファイルの日付と一致する日付を入力すると、その情報が出力されます。 (私はおそらく新しいスレッドwouldnt私を開始する必要がありますか?) –

関連する問題