Python:将逗号界定数据写入.csv文件的问题
目标:
- 从
.csv文件中读取股票价格数据
- 重新采样,
- 最终将输出打印到新的
.csv文件
,从而所有列以逗号分隔。
问题:
目前,程序可以 1) 读取数据并 2) 对其重新采样,但在打印到输出 .csv
时没有逗号分隔符。此外,列标题不打印在同一第一行。
输入数据示例:
*感兴趣的列:第一列(日期/时间)、第二列(买入价)和第四列(卖出价)。
20210602 22:02:00,3.07,50,3.086,50
20210602 22:03:00,3.07,50,3.087,50
20210602 22:04:00,3.071,50,3.087,50
20210602 22:04:00,3.071,50,3.088,50
20210602 22:05:00,3.07,50,3.088,50
20210602 22:05:00,3.07,50,3.087,50
20210602 22:06:00,3.071,50,3.087,50
当前输出:
open high low close
Date_time
2021-06-02 22:02:00 3.0790 3.0790 3.0780 3.0780
2021-06-02 22:03:00 3.0785 3.0785 3.0785 3.0785
2021-06-02 22:04:00 3.0790 3.0795 3.0790 3.0795
2021-06-02 22:05:00 3.0790 3.0790 3.0785 3.0785
2021-06-02 22:06:00 3.0790 3.0790 3.0790 3.0790
预期输出:
Date_time,open,high,low,close
2021-06-02 22:02:00,3.0790,3.0790,3.0780,3.0780
2021-06-02 22:03:00,3.0785,3.0785,3.0785,3.0785
2021-06-02 22:04:00,3.0790,3.0795,3.0790,3.0795
2021-06-02 22:05:00,3.0790,3.0790,3.0785,3.0785
2021-06-02 22:06:00,3.0790,3.0790,3.0790,3.0790
程序尝试:
import pandas as pd
import sys
data_frame = pd.read_csv('03-06-21-xngusd.csv',
names=['Date_time', 'Bid', 'Bid qty',
'Ask', 'Ask qty'],
index_col=0, parse_dates=True)
data_frame.head()
# Resample the data into 1 minute increments
data_ask = data_frame['Ask'].resample('1MIN').ohlc()
data_bid = data_frame['Bid'].resample('1MIN').ohlc()
data_mid = (data_ask + data_bid) / 2
data_mid.head()
# Print the new resample data to a .csv file
original_stdout = sys.stdout # Save reference to original standard output
with open('filename.csv', 'w') as f:
sys.stdout = f
print(data_mid)
sys.stdout = original_stdout
还尝试了以下代码,该代码仅打印第一行,并且还错过了显示的第一个列标题开盘价、最高价、最低价、收盘价
的输出:
with open('filename.csv', 'w') as writeFile:
writer = csv.writer(writeFile, delimiter=',')
writer.writerow(data_mid)
Q1.如何修改程序以确保输出列以逗号分隔
?
问题2.如何将列标题打印在文件顶部的同一行上?
Goal:
- Read stock price data in from a
.csv file
- resample it
- finally print the output to a new
.csv file
whereby all columns are comma delimited.
Issues:
Currently, the program can 1) read in the data and 2) resample it, but when printing to an output .csv
there are no comma delimiters. Furthermore, the column headers are not printed on the same first line.
Input data sample:
*Columns of interest: First column (which is date/time), second column (bid price), and fourth column (ask price).
20210602 22:02:00,3.07,50,3.086,50
20210602 22:03:00,3.07,50,3.087,50
20210602 22:04:00,3.071,50,3.087,50
20210602 22:04:00,3.071,50,3.088,50
20210602 22:05:00,3.07,50,3.088,50
20210602 22:05:00,3.07,50,3.087,50
20210602 22:06:00,3.071,50,3.087,50
Current output:
open high low close
Date_time
2021-06-02 22:02:00 3.0790 3.0790 3.0780 3.0780
2021-06-02 22:03:00 3.0785 3.0785 3.0785 3.0785
2021-06-02 22:04:00 3.0790 3.0795 3.0790 3.0795
2021-06-02 22:05:00 3.0790 3.0790 3.0785 3.0785
2021-06-02 22:06:00 3.0790 3.0790 3.0790 3.0790
Expected output:
Date_time,open,high,low,close
2021-06-02 22:02:00,3.0790,3.0790,3.0780,3.0780
2021-06-02 22:03:00,3.0785,3.0785,3.0785,3.0785
2021-06-02 22:04:00,3.0790,3.0795,3.0790,3.0795
2021-06-02 22:05:00,3.0790,3.0790,3.0785,3.0785
2021-06-02 22:06:00,3.0790,3.0790,3.0790,3.0790
Program attempt:
import pandas as pd
import sys
data_frame = pd.read_csv('03-06-21-xngusd.csv',
names=['Date_time', 'Bid', 'Bid qty',
'Ask', 'Ask qty'],
index_col=0, parse_dates=True)
data_frame.head()
# Resample the data into 1 minute increments
data_ask = data_frame['Ask'].resample('1MIN').ohlc()
data_bid = data_frame['Bid'].resample('1MIN').ohlc()
data_mid = (data_ask + data_bid) / 2
data_mid.head()
# Print the new resample data to a .csv file
original_stdout = sys.stdout # Save reference to original standard output
with open('filename.csv', 'w') as f:
sys.stdout = f
print(data_mid)
sys.stdout = original_stdout
Also tried the following code, which only printed the first row and also missed the first column header showing an output of open, high, low, close
:
with open('filename.csv', 'w') as writeFile:
writer = csv.writer(writeFile, delimiter=',')
writer.writerow(data_mid)
Q1. How can the program be modified to ensure that the output columns are comma delimited
?
Q2. How can the column headers be printed to be on the same line at the top of the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为此使用
data_frame.to_csv()
。阅读有关在这里。You can use
data_frame.to_csv()
for this. Read about it here.