当字段为0时,Mysql-Mariadb查询返回非类型



固定:帖子底部显示修复

我正在处理的管道部分查询mariadb表,获取值并将其添加到脚本另一部分的另一个值中。然后进行第二个查询以更新从中获取值的字段。有问题的表和列的默认值被设置为0,所以我不知道它为什么返回为nonetype。并非所有查询都会发生这种情况。该脚本使用joblib运行多个进程/线程,并且大多数查询都能工作。它处于try/except中,并在迭代后继续运行。命令行输出:

<class 'tuple'>
(1,)
<class 'tuple'>
(1,)
<class 'tuple'>
(10,)
<class 'tuple'>
(5,)
<class 'tuple'>
(2,)
<class 'tuple'>
(2,)
<class 'NoneType'>
None
ERROR:  wallstreetbets_04-02-2021.csv  : 'NoneType' object is not subscriptable  TICKER:  (4, datetime.datetime(2021, 4, 2, 0, 0), 'CME')
("'NoneType' object is not subscriptable",)

代码:

def runCountFinder(File):
try:
tic = time.perf_counter()
cnx = mysql.connector.connect(user='****', password='****', host='localhost', database='DataSAIL')
myCursor = cnx.cursor()
# print("Reading CSV ", File )
dataDF = pd.read_csv(r'/home/****/myoptane/Trawler/Dataframes/%s' % File, names=["Timestamp", "Subreddit", "Post/Comment"])

# print("Concatenating data ", File)
data = ''.join(map(str, dataDF['Post/Comment']))

# print("RUNNING FIND COUNTS ", File, flush=True)
result = findCounts.process_bodies(data)
result = findCounts.filter_pos_tokens(result, findCounts.target_pos_tags)
result = findCounts.count_tickers(result, findCounts.tickers)
# print("COMPLETED", flush=True)
# print("TRANSFERRING TICKER COUNTS TO DATAFRAME", File, flush=True)
resultDF = pd.DataFrame(list(result.items()), columns=['Ticker', 'Count'])
# print("COMPLETED", flush=True)
# resultDF.to_csv(r'D:GitlewisuDataSAILDataframestesting.csv', index=False)

# print("SAVING VALUES TO LISTS ", File, flush=True)
tickerList = resultDF['Ticker'].tolist()
countList = resultDF['Count'].tolist()
row_count = len(tickerList)
# dateList = dataDF['Timestamp'].tolist()
#
#
# dateFix = dateList[1]
#
# print(dateFix, " ", File)
dateFix = File.split("_", 1)[1]
dateFix = dateFix.split(".", 1)[0]
dateFix = dateFix + " 00:00:00"
dateFix = datetime.strptime(dateFix, "%m-%d-%Y %H:%M:%S")

# if len(str(dateList[1])) == 10:
#     dateFix = datetime.strptime(dateList[1], "%m/%d/%Y")
# else:
#     date_slice = dateList[1]
#     date_slice = date_slice[0:10]
#     dateFix = datetime.strptime(date_slice, "%Y-%m-%d")
# print(" dateFix COMPLETED ", dateFix, flush=True)
for i in range(0, row_count):
try:
sql1 = "SELECT mentions FROM Trawler WHERE date = %s AND stock = %s"
val1 = (dateFix, tickerList[i])
myCursor.execute(sql1, val1)
dbMentionCount = myCursor.fetchone()
# print(tickerList[i])
print(type(dbMentionCount))
print(dbMentionCount)

newCount = countList[i] + dbMentionCount[0]
# print(newCount)
# try:
#     # print("count(%d) + dbcount(%d)" % (countList[i], dbMentionCount[0]))
#     newCount = countList[i] + dbMentionCount[0]
# except Exception:
#     print("Failed to add counts ", File)
#     continue
sql = "Update Trawler SET mentions = %s WHERE date = %s AND stock = %s"
val = (newCount, dateFix, tickerList[i])
myCursor.execute(sql, val)
cnx.commit()
# print("ROW UPDATED # %d" % i)
except Exception as e:
print("Error: ", e, " Info: ", val, " count: ", countList[i], " dbcount: ", dbMentionCount[0])
cnx.close()
toc = time.perf_counter()
print("%s Completed***** in %0.4f seconds" % (File, (toc - tic)), flush=True)

except Exception as e:
print("ERROR: ", File, " :",e, " TICKER: ", val)
print(e.args)

更新CL输出,引发异常:

<class 'tuple'>
(0,)
(datetime.datetime(2021, 3, 23, 0, 0), 'CLOU')
<class 'tuple'>
(0,)
(datetime.datetime(2021, 3, 23, 0, 0), 'CME')
<class 'tuple'>
(0,)
(datetime.datetime(2021, 3, 23, 0, 0), 'CODX')
<class 'tuple'>
(0,)
(datetime.datetime(2021, 3, 23, 0, 0), 'COIN')
<class 'NoneType'>
None
ERROR:  stocks_03-23-2021.csv  : 'NoneType' object is not subscriptable  TICKER:  (4, datetime.datetime(2021, 3, 23, 0, 0), 'CODX')
joblib.externals.loky.process_executor._RemoteTraceback:
"""
Traceback (most recent call last):
File "main.py", line 102, in runCountFinder
newCount = countList[i] + dbMentionCount[0]
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/joblib/externals/loky/process_executor.py", line 418, in _process_worker
r = call_item()
File "/usr/local/lib/python3.6/site-packages/joblib/externals/loky/process_executor.py", line 272, in __call__
return self.fn(*self.args, **self.kwargs)
File "/usr/local/lib/python3.6/site-packages/joblib/_parallel_backends.py", line 600, in __call__
return self.func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/joblib/parallel.py", line 256, in __call__
for func, args, kwargs in self.items]
File "/usr/local/lib/python3.6/site-packages/joblib/parallel.py", line 256, in <listcomp>
for func, args, kwargs in self.items]
File "main.py", line 116, in runCountFinder
print("Error: ", e, " Info: ", val, " count: ", countList[i], " dbcount: ", dbMentionCount[0])
TypeError: 'NoneType' object is not subscriptable
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "main.py", line 142, in <module>
print(parallel([delayed(runCountFinder)(file) for file in fileList]), flush=True)
File "/usr/local/lib/python3.6/site-packages/joblib/parallel.py", line 1016, in __call__
self.retrieve()
File "/usr/local/lib/python3.6/site-packages/joblib/parallel.py", line 908, in retrieve
self._output.extend(job.get(timeout=self.timeout))
File "/usr/local/lib/python3.6/site-packages/joblib/_parallel_backends.py", line 554, in wrap_future_result
return future.result(timeout=timeout)
File "/usr/lib64/python3.6/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/lib64/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
TypeError: 'NoneType' object is not subscriptable

数据库表中的代码段:

| 87482641 | 2021-03-14 00:00:00 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 |        0 | CODX  |        2 |
| 87473610 | 2021-03-15 00:00:00 | 000000000000014.0000 | 000000000000014.3000 | 000000000000013.5600 | 000000000000013.7200 |   766080 | CODX  |        0 |
| 87473601 | 2021-03-16 00:00:00 | 000000000000013.7300 | 000000000000014.0000 | 000000000000013.1100 | 000000000000013.4600 |   513404 | CODX  |        0 |
| 87473592 | 2021-03-17 00:00:00 | 000000000000013.3700 | 000000000000013.8800 | 000000000000012.6700 | 000000000000013.6000 |   623469 | CODX  |        0 |
| 87473583 | 2021-03-18 00:00:00 | 000000000000013.5000 | 000000000000014.5000 | 000000000000013.4100 | 000000000000013.6900 |   813095 | CODX  |        0 |
| 87473574 | 2021-03-19 00:00:00 | 000000000000013.8500 | 000000000000014.1595 | 000000000000013.4000 | 000000000000014.0500 |   795922 | CODX  |        0 |
| 87482635 | 2021-03-20 00:00:00 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 |        0 | CODX  |        0 |
| 87482633 | 2021-03-21 00:00:00 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 |        0 | CODX  |        0 |
| 87473565 | 2021-03-22 00:00:00 | 000000000000014.0500 | 000000000000014.9700 | 000000000000014.0250 | 000000000000014.5300 |   840116 | CODX  |        0 |
| 87473556 | 2021-03-23 00:00:00 | 000000000000014.6600 | 000000000000014.6728 | 000000000000013.4400 | 000000000000013.6100 |   766312 | CODX  |        4 |
| 87473547 | 2021-03-24 00:00:00 | 000000000000013.8100 | 000000000000013.9100 | 000000000000012.3400 | 000000000000012.4000 |   912711 | CODX  |        0 |
| 87473538 | 2021-03-25 00:00:00 | 000000000000012.1648 | 000000000000012.6420 | 000000000000011.8000 | 000000000000012.1200 |  1650841 | CODX  |        0 |
| 87473529 | 2021-03-26 00:00:00 | 000000000000011.1100 | 000000000000011.1300 | 000000000000009.5700 | 000000000000009.7000 |  3822083 | CODX  |        0 |
| 87482627 | 2021-03-27 00:00:00 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 |        0 | CODX  |        0 |
| 87482621 | 2021-03-28 00:00:00 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 | 000000000000000.0000 |        0 | CODX  |        0 |

因此,问题确实是没有一行与传入的值匹配,但让我失望的是,我打印的错误消息显示了错误的信息。出于某种原因,我将查看的错误消息显示了值的下一次迭代,而不是出现错误的值。在意识到我检查了值之后,实际上出现了错误,并发现缺少该行。对于那些感兴趣的人。这些行之所以缺失,是因为填充表的脚本部分为股票行情机获取股票行情机信息,而对于这些股票行情机,它们在纳斯达克上市的时间不长,所以现在有了针对这些日期的行。

所以问题确实是没有一行与传入的值匹配,但让我失望的是我打印的错误消息显示了错误的信息。出于某种原因,我将查看的错误消息显示了值的下一次迭代,而不是出现错误的值。在意识到我检查了值之后,实际上出现了错误,并发现缺少该行。对于那些感兴趣的人。这些行之所以缺失,是因为填充表的脚本部分为股票行情机获取股票行情机信息,而对于这些股票行情机,它们在纳斯达克上市的时间不长,所以现在有了针对这些日期的行。

最新更新