Python:将逗号界定数据写入.csv文件的问题

发布于 2025-01-18 01:42:29 字数 2458 浏览 4 评论 0原文

目标:

  1. .csv文件中读取股票价格数据
  2. 重新采样,
  3. 最终将输出打印到新的.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:

  1. Read stock price data in from a .csv file
  2. resample it
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2025-01-25 01:42:29

您可以为此使用data_frame.to_csv()。阅读有关在这里

You can use data_frame.to_csv() for this. Read about it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文