Python TypeError:join()参数必须是str或bytes,而不是';非类型';
我不断得到下面的错误,有人能告诉我我做错了什么吗Python TypeError:join()参数必须是str或bytes,而不是';非类型';,python,Python,我不断得到下面的错误,有人能告诉我我做错了什么吗 Traceback (most recent call last): File "generate_simulated_pair.py", line 50, in <module> outfile = open(os.path.join(settings.CSV_DATA_DIR ,"%s_%s.csv"%(pair, d.strftime ("%Y%m%d") File "C:\Program Files\Pytho
Traceback (most recent call last):
File "generate_simulated_pair.py", line 50, in <module>
outfile = open(os.path.join(settings.CSV_DATA_DIR ,"%s_%s.csv"%(pair, d.strftime ("%Y%m%d")
File "C:\Program Files\Python35\lib\ntpath.py", line 113, in join
genericpath._check_arg_types('join', path, *paths)
File "C:\Program Files\Python35\lib\genericpath.py", line 143, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'NoneType'
回溯(最近一次呼叫最后一次):
文件“generate_simulated_pair.py”,第50行,在
outfile=open(os.path.join(settings.CSV\u DATA\u DIR,“%s\u%s.CSV”%”(pair,d.strftime(“%Y%m%d”)
文件“C:\Program Files\Python35\lib\ntpath.py”,第113行,在连接中
常规路径。检查参数类型('join',path,*path)
文件“C:\Program Files\Python35\lib\genericpath.py”,第143行,在检查参数类型中
(funcname,s._______;类___;名称__;))来自无
TypeError:join()参数必须是str或bytes,而不是'NoneType'
请参见下文,这是用于从oanda创建CSV文件以进行回溯测试
from __future__ import print_function
import calendar
import copy
import datetime
import os, os.path
import sys
import numpy as np
import pandas as pd
import pandas_datareader
from pandas_datareader import data, wb
from qsforex import settings
def month_weekdays(year_int, month_int):
"""
Produces a list of datetime.date objects representing the
weekdays in a particular month, given a year.
"""
cal = calendar.Calendar()
return [d for d in cal.itermonthdates(year_int, month_int)if d.weekday() < 5 and d.year == year_int]
if __name__ == "__main__":
try:
pair = sys.argv[1]
except IndexError:
print("You need to enter a currency pair, e.g. GBPUSD, as a command line parameter.")
else:
np.random.seed(42) # Fix the randomness
S0 = 1.5000
spread = 0.002
mu_dt = 1400 # Milliseconds
sigma_dt = 100 # Millseconds
ask = copy.deepcopy(S0) + spread / 2.0
bid = copy.deepcopy(S0) - spread / 2.0
days = month_weekdays(2014, 1) # January 2014
current_time = datetime.datetime(
days[0].year, days[0].month, days[0].day, 0, 0, 0,
)
# Loop over every day in the month and create a CSV file
# for each day, e.g. "GBPUSD_20150101.csv"
for d in days:
print(d.day)
current_time = current_time.replace(day=d.day)
outfile = open(os.path.join(settings.CSV_DATA_DIR ,"%s_%s.csv"%(pair, d.strftime ("%Y%m%d")
)
),
"wb")
outfile.write("Time,Ask,Bid,AskVolume,BidVolume\n")
# Create the random walk for the bid/ask prices
# with fixed spread between them
while True:
dt = abs(np.random.normal(mu_dt, sigma_dt))
current_time += datetime.timedelta(0, 0, 0, dt)
if current_time.day != d.day:
outfile.close()
break
else:
W = np.random.standard_normal() * dt / 1000.0 / 86400.0
ask += W
bid += W
ask_volume = 1.0 + np.random.uniform(0.0, 2.0)
bid_volume = 1.0 + np.random.uniform(0.0, 2.0)
line = "%s,%s,%s,%s,%s\n" % (
current_time.strftime("%d.%m.%Y %H:%M:%S.%f")[:-3],
"%0.5f" % ask, "%0.5f" % bid,
"%0.2f00" % ask_volume, "%0.2f00" % bid_volume
)
outfile.write(line)
from\uuuuu future\uuuuu导入打印功能
导入日历
导入副本
导入日期时间
导入操作系统,操作系统路径
导入系统
将numpy作为np导入
作为pd进口熊猫
导入数据读取器
从数据读取器导入数据,wb
从qsforex导入设置
def月日(年日、月日):
"""
生成一个datetime.date对象列表,这些对象表示
一年中特定月份的工作日。
"""
cal=calendar.calendar()
如果d.weekday()小于5且d.year==year\u int,则返回[d在cal.itermonthdates(year\u int,month\u int)中表示d]
如果名称=“\uuuuu main\uuuuuuuu”:
尝试:
pair=sys.argv[1]
除索引器外:
打印(“您需要输入一个货币对,例如GBP USD,作为命令行参数。”)
其他:
np.random.seed(42)#修复随机性
S0=1.5000
价差=0.002
mu_dt=1400毫秒
sigma#u dt=100百万秒
ask=复制。深度复制(S0)+排列/2.0
投标=副本。深度副本(S0)-排列/2.0
天=月\工作日(2014,1)\ 2014年1月
当前时间=datetime.datetime(
天[0]。年,天[0]。月,天[0]。天,0,0,0,
)
#循环每月的每一天,并创建一个CSV文件
#每一天,例如“英镑USD_20150101.csv”
对于d(以天为单位):
打印(d日)
当前时间=当前时间。替换(天=天)
outfile=open(os.path.join(settings.CSV\u DATA\u DIR,“%s\u%s.CSV”%”(pair,d.strftime(“%Y%m%d”)
)
),
“世界银行”)
outfile.write(“时间、询问、出价、AskVolume、出价卷\n”)
#为出价/要价创建随机游走
#他们之间有固定的价差
尽管如此:
dt=abs(np.随机.正态(mu_dt,sigma_dt))
当前时间+=datetime.timedelta(0,0,0,dt)
如果当前_time.day!=d.day:
outfile.close()
打破
其他:
W=np.随机.标准_标准()*dt/1000.0/86400.0
ask+=W
投标价+=W
ask_体积=1.0+np.随机.均匀(0.0,2.0)
投标量=1.0+np.随机.均匀(0.0,2.0)
line=“%s,%s,%s,%s,%s\n”%(
当前时间.strftime(“%d.%m.%Y%H:%m:%S.%f”)[:-3],
“%0.5f”%ask、“%0.5f”%bid、,
“%0.2f00”%ask\U卷“%0.2f00”%bid\U卷
)
输出文件。写入(行)
settings.CSV\u DATA\u DIR
为无,因此os.path.join(settings.CSV\u DATA\u DIR,…)
失败
为便于将来参考,请务必查看回溯并按照堆栈返回到您自己的代码;回溯从以下内容开始:
File "generate_simulated_pair.py", line 50, in <module>
outfile = open(os.path.join(settings.CSV_DATA_DIR ,"%s_%s.csv"%(pair, d.strftime ("%Y%m%d")
因此,os.path.join()
的一个参数是一个None
值。在您传入的两个参数中,第二个参数绝对是一个字符串,只剩下一个选项:另一个参数必须是None
join() argument must be str or bytes, not 'NoneType'