python - 解析后我无法将数据写入文件,我不断收到错误字符串索引必须是整数
问题描述
我有 stocktwits 数据,我正在尝试通过标记来转换文本文件。我正在尝试使用 .json 读取 json 格式json.loads(line)
。
import json
with open(self.trainables_path + file_to_write, "w") as fp:
for doc in enumerate(files):
with open(self.tweets_path + doc[1]) as f:
for line in f:
entry = json.loads(line)
user = list(entry.keys())[0]
tweet = entry[user]
fp.write(user + "_" + str(tweet['id']) + " ")
for token in tweet['tokens']:
if tweet['tokens'].index(token) != len(tweet['tokens']) - 1:
fp.write(token + " ")
else:
fp.write(token)
fp.write("\n")
这是错误:
Traceback (most recent call last):
File "H:/dissertation/rec-sys-master/embeddings/parser.py", line 201, in <module>
pd.convert_to_trainable()
File "H:/dissertation/rec-sys-master/embeddings/parser.py", line 169, in convert_to_trainable
fp.write(user + "_" + str(tweet['id']) + " ")
TypeError: string indices must be integers
数据需要写入文件。
编辑:代码中的其他函数没有被执行,这就是数据格式不正确的原因!!
解决方案
推荐阅读
- android - 我该怎么办,我希望每个人都可以读取 firebase 的数据,但要由同一身份验证人删除数据
- php - Laravel Eloquent - 按每种类型选择最大的 - join、max() 和 group by
- java - 如何在 MacOS 中确定 JRE 路径
- javascript - 从 React Js 中的数组列表获取输入字段
- javascript - 如何使用 JQuery 提取与模式匹配的子字符串
- google-maps - 在谷歌地图标记上添加一个弹出窗口
- python - 如果 Alembic 已经定义了架构,那么使用 sqlalchemy 定义的架构有什么关系?
- ios - 后台线程在主线程 ios swift 之前执行
- react-native - 更改存储状态后导航到初始路由
- java - 如何在 Java 中关闭和“销毁”对象是正确的