[英]How To Read Lines of CSV or Text File, Loop Over Each Line and Save To a New File For Each Line Read
[英]Read and process a text file and save to csv
我的文件似乎是“dict”格式......
文件頭如下: time,open,high,low,close,volume
下一行如下:{“t”:[1494257340],“o”:[206.7],“h”:[209.3],“l”:[204.50002],“c”:[204.90001],“v” :[49700650]}`
import csv
with open ('test_data.txt', 'rb') as f:
for line in f:
dict_file = eval(f.read())
time = (dict_file['t']) # print (time) result [1494257340]
open_price = (dict_file['o']) # print (open_price) result [206.7]
high = (dict_file['h']) # print (high) result [209.3]
low = (dict_file['l']) # print (low) result [204.50002]
close = (dict_file['c']) # print (close) result [204.90001]
volume = (dict_file['v']) # print (volume) result [49700650]
print (time, open_price, high, low, close, value)
# print result [1494257340] [206.7] [209.3] [204.50002] [204.90001] [49700650]
# I need to remove the [] from the output.
# expected result
# 1494257340, 206.7, 209.3, 204.50002, 204.90001, 49700650
我需要的結果是(將時間(“紀元日期格式”)改為dd,mm,yy
5/8/17, 206.7, 209.3, 204.50002, 204.90001, 49700650
所以我知道我需要csv.writer函數
我在您提交的代碼中看到了許多問題。 我建議你把你的任務分成小塊,看看你是否可以讓它們單獨工作。 那么你想要做的是:
eval
每一行以獲得一個dict
對象 對?
現在做每一個,當時只有一小步
你在那里非常重要:
with open('test_data.txt', 'rb') as f:
print(f.read())
# b'{"t":[1494257340],"o":[207.75],"h":[209.8],"l":[205.75],"c":[206.35],"v":[61035956]}\n'
您可以在r
模式下打開文件,它將為您提供字符串而不是byte
類型對象
with open('test_data.txt', 'r') as f:
print(f.read())
# {"t":[1494257340],"o":[207.75],"h":[209.8],"l":[205.75],"c":[206.35],"v":[61035956]}
它可能會導致一些問題但是應該可以工作,因為eval可以很好地處理它(至少在python 3中)
with open('test_data.txt', 'rb') as f:
for line in f:
print(line)
# b'{"t":[1494257340],"o":[207.75],"h":[209.8],"l":[205.75],"c":[206.35],"v":[61035956]}\n'
這是你的代碼中的另一個問題,你沒有使用line
變量並試圖改為f.read()
。 這將只讀取整個文件(從第二行開始,因為已經讀取了第一行)。 嘗試互換一個,看看會發生什么
eval
每一行以獲得一個dict
對象 再次。 這很好用。 但我會在這里加一些保護。 如果您在文件中找到空行或格式錯誤的行,該怎么辦? 此外,如果此文件來自不受信任的來源,您可能會成為代碼注入的受害者,例如文件中的行更改為:
print("You've been hacked") or {"t":[1494257340],"o":[207.75],"h":[209.8],"l":[205.75],"c":[206.35],"v":[61035956]}
with open('test_data.txt', 'rb') as f:
for line in f:
dict_file = eval(line)
print(dict_file)
# You've been hacked
# {'t': [1494257340], 'o': [207.75], 'h': [209.8], 'l': [205.75], 'c': [206.35], 'v': [61035956]}
我不知道你的具體規格,但你應該用json.loads
更安全。
...
你可以從那里繼續自己嗎?
我認為dict_file['t']
沒有給你你期望的價值。
它給你帶來了什么?
為什么?
怎么解決?
你能把一些隨機字符串寫入文件嗎?
scv格式是什么樣的? 您可以格式化您的值以匹配它
檢查csv
模塊的文檔,它對你有幫助嗎?
等等等等...
編輯:解決方案
# you can save the print output in a file by running:
# $ python convert_to_csv.py > output.cvs
import datetime, decimal, json, os
CSV_HEADER = 'time,open,high,low,close,volume'
with open('test_data.txt', 'rb') as f:
print(CSV_HEADER)
for line in f:
data = json.loads(line, parse_float=decimal.Decimal)
data['t'][0] = datetime.datetime.fromtimestamp(data['t'][0]) \
.strftime('%#d/%#m/%y' if os.name == 'nt' else '%-d/%-m/%y')
print(','.join(str(data[k][0]) for k in 'tohlcv'))
運行:
$ cat test_data.txt
{"t":[1494257340],"o":[207.75],"h":[209.8],"l":[205.75],"c":[206.35],"v":[61035956]}
{"t":[1490123123],"o":[107.75],"h":[109.8],"l":[105.75],"c":[106.35],"v":[11035956]}
{"t":[1491234234],"o":[307.75],"h":[309.8],"l":[305.75],"c":[306.35],"v":[31035956]}
$ python convert_to_csv.py
time,open,high,low,close,volume
8/5/17,207.75,209.8,205.75,206.35,61035956
21/3/17,107.75,109.8,105.75,106.35,11035956
3/4/17,307.75,309.8,305.75,306.35,31035956
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.