2016-05-21 8 views
0

私はdata.txtファイルに保存した販売記録を照会しようとしています。各行はカンマで区切られます。ファイルの内容とクエリデータを読み取ります。 python 3.5.1

<date>,<branch_name>,<total_daily_sale>,<number_of_daily_transactions>

これはdata.txtをファイル内の情報です。

私は値を返す関数 readdata(filename)を書いても、テンプレートをフォーマットし、青で下のスクリーンショットのようにそれを表示するには、非値を返す目的球 print_all_records(records)を書きたい
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 

enter image description here

また、私はSQLiteデータを利用して助けてくれたコードもあります。しかし、私はSQLiteを使用する以外の方法があることを願っています。

import sqlite3 
conn = sqlite3.connect('data.db') 
c = conn.cursor() 
c.execute('''CREATE TABLE data 
     (field1 text, field2 text)''') 

data = open('data.txt') 
items1 = [] 
items2 = [] 
items3 = [] 
data2 = open('outs.txt') 

for item in data: 
try: 
    (item1,item2) = item.strip().split(',',1) 

    items1.append('item1') 
    items2.append('item2') 
    c.execute("insert into data (field1,field2) values (?,?)", 
     (item1,item2)) 
    conn.commit()  

except: 
    pass 

conn.close() 

print ('All item successfully stored in database') 
+1

テキストの画像を使用しないでください。 – Zulan

+0

行/行は改行で区切られ、列はカンマで区切られています:)。 – user161778

答えて

0

私はpandasをお勧めします。

import pandas as pd 
df = pd.read_csv("data.txt",header=None,sep=",") 
df.columns = ["Date","Branch","Sale","Transaction"] 
print(df) 

出力

Index Date  Branch Sale  Transaction  
0  1-2-2014 Frankton 42305.67 23  
1  12-4-2014 Glenview 21922.22 17  
... 
関連する問題