Python行为在脚本和命令行中有所不同
当我键入“python”进入python命令行并执行此脚本时,它工作得非常完美,并且完全完成了我需要它完成的任务:Python行为在脚本和命令行中有所不同,python,json,python-2.7,dictionary,ebay-api,Python,Json,Python 2.7,Dictionary,Ebay Api,当我键入“python”进入python命令行并执行此脚本时,它工作得非常完美,并且完全完成了我需要它完成的任务: import json import datetime dictstr = {'FeedbackScore': '884', 'IDVerified': 'false', 'eBayGoodStanding': 'true', 'AboutMePage': 'false', 'UserSubscription': ['FileExchange'], 'UserIDChanged'
import json
import datetime
dictstr = {'FeedbackScore': '884', 'IDVerified': 'false', 'eBayGoodStanding': 'true', 'AboutMePage': 'false', 'UserSubscription': ['FileExchange'], 'UserIDChanged': 'false', 'PayPalAccountType': 'Business', 'PositiveFeedbackPercent': '100.0', 'Email': 'xxxxxxxxx', 'EIASToken': 'xxxxxxxxxxxxxxx', 'PayPalAccountStatus': 'Active', 'UniquePositiveFeedbackCount': '66', 'UniqueNeutralFeedbackCount': '0', 'SellerInfo': {'CheckoutEnabled': 'true', 'TransactionPercent': '69.0', 'StoreOwner': 'false', 'AllowPaymentEdit': 'false', 'RecoupmentPolicyConsent': None, 'PaymentMethod': 'PayPal', 'GoodStanding': 'true', 'SafePaymentExempt': 'true', 'SellerGuaranteeLevel': 'NotEligible', 'LiveAuctionAuthorized': 'false', 'MerchandizingPref': 'OptIn', 'CIPBankAccountStored': 'false', 'QualifiesForB2BVAT': 'false', 'SchedulingInfo': {'MaxScheduledMinutes': '30240', 'MaxScheduledItems': '3000', 'MinScheduledMinutes': '0'}, 'CharityRegistered': 'false'}, 'UserIDLastChanged': datetime.datetime(2014, 8, 6, 0, 37, 37), 'EnterpriseSeller': 'false', 'Status': 'Confirmed', 'PayPalAccountLevel': 'Verified', 'UserID': 'xxxxxxxxxxxx', 'eBayWikiReadOnly': 'false', 'FeedbackRatingStar': 'Purple', 'UniqueNegativeFeedbackCount': '0', 'VATStatus': 'NoVATTax', 'MotorsDealer': 'false', 'RegistrationDate': datetime.datetime(2003, 1, 12, 0, 21, 42), 'BusinessRole': 'FullMarketPlaceParticipant', 'Site': 'US', 'EBaySubscription': 'FileExchange', 'FeedbackPrivate': 'false', 'NewUser': 'false'}
f = open('json_output.txt','w')
dump_file = json.dumps(dictstr, indent=4, default=str)
f.write(dump_file)
f.close()
但是,当我将其作为脚本运行时,输出文件的内容都在一行上:
import json
import datetime
import ebaysdk
from ebaysdk.trading import Connection as Trading
def getUser():
api = Trading(config_file='ebay.yaml')
f = open('json_output.txt','w')
api.execute('GetUser', {'UserID': 'xxxxxxxxx'})
dictstr = "%s" % api.response.reply.User
print dictstr
dump_file = json.dumps(dictstr, indent=4, default=str)
f.write(dump_file)
f.close()
if __name__ == "__main__":
getUser()
文件都在一行上,就像字典的原始声明一样
我在非工作示例中使用stdout的内容来填充工作示例中的dictstr变量。我这样做是因为我想确保我使用的是完全相同的数据
各位专家知道我做错了什么吗?我很高兴事情这么简单 更正代码:
import json
import datetime
import ebaysdk
from ebaysdk.trading import Connection as Trading
def getUser():
api = Trading(config_file='ebay.yaml')
f = open('json_output.txt','w')
api.execute('GetUser', {'UserID': 'xxxxxxxxx'})
dictstr = api.response.dict()
dump_file = json.dumps(dictstr, indent=4, default=str)
f.write(dump_file)
f.close()
if __name__ == "__main__":
getUser()
在第一种情况下,
dictstr
是一个dict。在第二种情况下,dictstr
是一个字符串。