首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Pymongo将字符串输入到MongoDB中

使用Pymongo将字符串输入到MongoDB中
EN

Stack Overflow用户
提问于 2015-06-11 04:18:33
回答 1查看 1.5K关注 0票数 0

我试图让用户输入数据来执行插入,只要我有数字,它就能工作,但是当我输入字母时,它给了我错误"LettersUsed“的定义。我尝试将输入转换为str(输入(“任何”)),但这并没有任何帮助,为什么要这样做?

代码语言:javascript
复制
import pymongo
import sys

#get a connection to database
connection = pymongo.MongoClient('mongodb://localhost')
    #get a handle to database
db=connection.test
vehicles=db.vehicles

vehicle_VIN = input('What is the Vehicle VIN number? ')
vehicle_Make = input('What is the Vehicle Make? ')
newVehicle = {'VIN' : (vehicle_VIN).upper(), 'Make' : (vehicle_Make)}

try:
    vehicles.insert_one(newVehicle)
    print ('New Vehicle Inserted')

except  Exception as e:
        print 'Unexpected error:', type(e), e

#print Results
results = vehicles.find()

print()
# display documents in collection
for record in results:
    print(record['VIN'] + ',',record['Make'])
#close the connection to MongoDB
connection.close()
EN

Stack Overflow用户

发布于 2015-06-11 04:57:30

消息:未定义名称“DAFEQF”

在Python2中,代码input()等于eval(raw_input(prompt))。这意味着无论您输入什么输入,Python2都会尝试“评估”该输入,并会抱怨您的输入没有定义。

确保将input()替换为raw_input() (这仅用于Python2!)

替换

代码语言:javascript
复制
vehicle_VIN = input('What is the Vehicle VIN number? ')
vehicle_Make = input('What is the Vehicle Make? ')

使用

代码语言:javascript
复制
vehicle_VIN = raw_input('What is the Vehicle VIN number? ')
vehicle_Make = raw_input('What is the Vehicle Make? ')
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30771488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档