[英]python list with true and false not True and False
- - - - - - - - - - - - 更新 - - - - - - - - - - -
由于存在太多混乱,我决定给出更详细的解释。 看看下面的代码,并专注于
day = {"days": buildString2(day_array[i])}
这是代码:
import csv, sys, requests, json, os, itertools, ast
def buildString(item):
item_array = item.split(",")
mod = []
for i in range(len(item_array)):
mod.append("%s" % item_array[i].strip())
return mod
def buildString2(item):
item_array = item.split(",")
mod = "["
for i in range(len(item_array)):
if i == len(item_array) - 1:
mod = mod + '%s' % item_array[i].strip()
else:
mod = mod + '%s, ' % item_array[i].strip()
mod = mod + "]"
return mod
if __name__ == '__main__':
def main():
filename = 'file.csv'
dict = {"id":'c8d5185667f'}
with open(filename, 'r', encoding='utf8') as f:
reader = csv.reader(f)
try:
count = 0
for row in reader:
count = count + 1
if count != 1:
dict["name"] = row[10]
dict["space_usages"] = buildString(row[19])
availablle_array = []
available_booking_hours = row[15]
days = row[18]
availability_array = available_booking_hours.split("*")
day_array = days.split("*")
for i in range(len(day_array)):
startEndTime = availability_array[i].split("-")
day = {"days": buildString2(day_array[i])}
times = {"start_time":startEndTime[0], "end_time":startEndTime[1]}
day["times"] = times
availablle_array.append(day)
dict["available_days"] = availablle_array
print(dict)
url = 'http://50.97.247.68:9000/api/v1/spaces'
response = requests.post(url, data=json.dumps(dict))
当我打印字典时,得到以下内容
{'id': 'c8d5185667f', 'available_days': [{'days': '[true, true, true, true, true, true, true]', 'times': {'start_time': '12:00', 'end_time': '10:00'}}], 'space_usages': ['Fitness', 'Events', 'Classes', 'Performance']}
但是我老板要这个
{'id': 'c8d5185667f', 'available_days': [{'days': [true, true, true, true, true, true, true], 'times': {'start_time': '12:00', 'end_time': '10:00'}}], 'space_usages': ['Fitness', 'Events', 'Classes', 'Performance']}
这也不起作用
{'id': 'c8d5185667f', 'available_days': [{'days': ['true', 'true', 'true', 'true', 'true', 'true', 'true'], 'times': {'start_time': '12:00', 'end_time': '10:00'}}], 'space_usages': ['Fitness', 'Events', 'Classes', 'Performance']}
这更有意义吗? 有可能得到
[true, true, true, true, true, true, true]
作为价值? 我尝试这样做
day = {"days": ast.literal_eval(buildString2(day_array[i]))}
但它崩溃了。 我没主意了。 我尝试使用Google搜索各种内容,但似乎找不到任何东西。 非常感谢您的帮助。 老实说,我不相信这是可能的,但这就是我被告知要做的。
注意:它们必须为小写。 这不行
[True, True, True, True, True, True, True]
这是JSON,因此您应该将一周转换为JSON格式
In [1]: import simplejson as json
In [2]: week = [True, False, True, True]
In [3]: json.dumps(week)
Out[3]: '[true, false, true, true]'
要转换回去,只需加载并解析它:
In [8]: print json.loads('[true, false, false, true]')
[True, False, False, True]
您可以使用json
模块将布尔值列表转换为字符串并相互转换:
>>> import json
>>> json.dumps([True, False, True, True, False])
'[true, false, true, true, false]'
>>> json.loads('[true, false, true, true, false]')
[True, False, True, True, False]
你的价值是
week = '[true, false, true, false, true, false, true]'
这是JSON表示形式。 第二个值是
week = ['true', 'false', 'true', 'false', 'true', 'false', 'true']
这是带有“ true”和“ false”字符串的列表。
week = [True, False, True, False, True, False, True]
这是纯python代码。
您必须决定要拥有的值以及选择以哪种方式将其转换为哪种形式的值。
对于第一个值,您可以使用json.loads
。
对于第二个值,您必须手动检查字符串“ true”和“ false”
第三是仅python,因此无需再次在python中进行更改:)。
这有可能让你的老板想什么,只是不使用内置的真假:
class MyBool(object):
def __init__(self, value):
self.value = bool(value)
def __repr__(self):
return repr(self.value).lower()
def __bool__(self):
return self.value
print({'a' : [MyBool(True), MyBool(True), MyBool(False)]})
结果:
{'a': [true, true, false]}
您实际上并不需要__bool__
,但是(在Python 3中)它允许在逻辑条件下使用对象。
因此,根据要求,它不是有效的Python文字,因为它使用true
而不是True
,并且它不是有效的JSON,因为它使用单引号的字符串而不是双引号。 大概是由不接受True
且不接受双引号字符串的内容解析的吗? 我不认为有可能仅修复原始API来接受JSON吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.