我有json,它包含下一个键
[u'domain', u'_timestamp', u'meta_tags', u'author', u'title', u'url', u'tags', u'flow', u'link_tags', u'content', u'post_id', u'flags', u'polling', u'published', u'hubs', u'_id']
我需要将它写入.vw
文件。
但其中一些是数字的,还有一些字符串。我需要保存这些类型。
此外,我的文件的值为target
url target
vk.com 0.934250
我用
targets = train_target.target.values.tolist()
with open('train.json') as inp_json, \
open('habr_train.vw', 'w') as out_vw:
for i, line in enumerate(tqdm_notebook(inp_json)):
data_json = json.loads(line)
if data_json['flow'] is None and data_json['author']['nickname'] is None:
res_line = str(targets[i]) + ' |title ' + data_json['title'] + ' |tags ' + ' '.join(data_json['tags']) \
+ ' |domain ' + data_json['domain'] + ' |flow None' + ' |author None' + ' |hubs ' + data_json['hubs'][0]['title'] + ' |num content_len:' + str(round(len(data_json['content']) / 1000000, 1)) + ' month:' + str(datetime.fromtimestamp(data_json['_timestamp']).month) + ' hour:' + str(datetime.fromtimestamp(data_json['_timestamp']).hour) + '\n'
elif data_json['flow'] is None:
res_line = str(targets[i]) + ' |title ' + data_json['title'] + ' |tags ' + ' '.join(data_json['tags']) \
+ ' |domain ' + data_json['domain'] + ' |flow None' + ' |author ' + data_json['author']['nickname'] + ' |hubs ' + data_json['hubs'][0]['title'] + ' |num content_len:' + str(round(len(data_json['content']) / 1000000, 1)) + ' month:' + str(datetime.fromtimestamp(data_json['_timestamp']).month) + ' hour:' + str(datetime.fromtimestamp(data_json['_timestamp']).hour) + '\n'
elif data_json['author']['nickname'] is None:
res_line = str(targets[i]) + ' |title ' + data_json['title'] + ' |tags ' + ' '.join(data_json['tags']) \
+ ' |domain ' + data_json['domain'] + ' |flow ' + data_json['flow'] + ' |author None' + ' |hubs ' + data_json['hubs'][0]['title'] + ' |num content_len:' + str(round(len(data_json['content']) / 1000000, 1)) + ' month:' + str(datetime.fromtimestamp(data_json['_timestamp']).month) + ' hour:' + str(datetime.fromtimestamp(data_json['_timestamp']).hour) + '\n'
else:
res_line = str(targets[i]) + ' |title ' + data_json['title'] + ' |tags ' + ' '.join(data_json['tags']) \
+ ' |domain ' + data_json['domain'] + ' |flow ' + data_json['flow'] + ' |author ' + data_json['author']['nickname'] + ' |hubs ' + data_json['hubs'][0]['title'] + ' |num content_len:' + str(round(len(data_json['content']) / 1000000, 1)) + ' month:' + str(datetime.fromtimestamp(data_json['_timestamp']).month) + ' hour:' + str(datetime.fromtimestamp(data_json['_timestamp']).hour) + '\n'
out_vw.write(res_line.encode('utf-8'))
它有效,但接下来我需要使用库并且它返回错误,str(targets [i])应该是float。
有什么方法可以保存值的类型? 我该如何解决这个问题?
答案 0 :(得分:0)
而不是使用连接运算符,您可以使用format
来避免这种错误
示例强>:
res_line = '{0} |title {1} |tags {2} |domain {3} |flow None |author None |hubs {4} |num content_len: {5} month: {6} hour: {7}\n'.format(str(targets[i]),data_json['title'], ' '.join(data_json['tags']), data_json['domain'], data_json['hubs'][0]['title'], str(datetime.fromtimestamp(data_json['_timestamp']).month), str(datetime.fromtimestamp(data_json['_timestamp']).hour))