首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python行为在脚本和命令行中有所不同

Python行为在脚本和命令行中有所不同
EN

Stack Overflow用户
提问于 2018-06-15 04:53:05
回答 1查看 39关注 0票数 -3

当我输入" python“进入python命令行并执行这个脚本时,它工作得很好,完全符合我的要求:

代码语言:javascript
复制
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()

但是,当我将其作为脚本运行时,输出文件的所有内容都在一行中:

代码语言:javascript
复制
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变量。我这样做是因为我想确保我使用的是完全相同的数据。

你们专家知道我哪里做错了吗?

EN

回答 1

Stack Overflow用户

发布于 2018-06-15 05:46:35

我很高兴它是这么简单的东西。

已更正代码:

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50865770

复制
相关文章

相似问题

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