小编Jon*_*nts的帖子

在Python中获取异常详细信息

我必须在同一个循环中打开和写入大约10个不同的文件.例如:

for i in range(0,10):
    try:
        a=5
        file1 = open("file1.txt",'w+')
        file2 = open("file2.txt",'w+')
        #... etc

        print(str(a),file=file1)
        print(str(a)+"hi",file=file2)
        # ... etc
    except: 
        #error handling
Run Code Online (Sandbox Code Playgroud)

现在我想要做的是能够获得特定的异常信息,例如在一般异常中打开/写入的文件.根据我目前的理解,我必须做这样的事情来实现我想要的:

for i in range(0,5):
    a=5
    try:
        file1 = open("file1.txt",'w+')
        print(str(a),file=file1)
    except: 
        #error handling for file1
    try:
        file2 = open("file2.txt",'w+')
        print(str(a)+"hi",file=file2)
    except: 
        #error handling for file2
Run Code Online (Sandbox Code Playgroud)

...当我必须为大约10个不同的文件执行此操作时,哪个会变得非常笨重且没有吸引力.有没有办法从我的第一个例子中获取(例如)一般异常中的文件名信息?基本上,异常可以报告诸如"写入文件1时的错误"而没有尝试/除了专门用于file1操作.

编辑:这是对写入文件的数据的过度简化.str(a)和str(a)+"hi"对实际写入的数据并不是很好的表示; file1可能需要一个硬编码整数,其中file2可能需要一个用多个变量格式化的字符串.将开放/写入过程概括为循环并不会很好地工作.

python error-handling exception python-3.3

18
推荐指数
3
解决办法
4万
查看次数

如何检查列表中是否有特定的整数

我想知道如果某个整数在列表中,如何创建一个执行子句的if语句.

我见过的所有其他答案都要求质素,重复等特定条件,我无法从其他人那里收集问题的解决方案.

python int if-statement list python-2.7

17
推荐指数
2
解决办法
6万
查看次数

返回最接近用户定义数字的数据框中的行

我有一个用户定义的数字,我想与数据帧的某一列进行比较.

我想返回一个数据帧的行,它包含(在df的某一列中,比如df.num)与给定数字x最接近的5个数字.

任何关于没有循环的最佳方法的建议将非常感激.

python python-2.7 pandas

17
推荐指数
1
解决办法
1万
查看次数

Python类__div__问题

元组代表分数.我试图通过乘以收益来划分分数

class Test():
    def __init__(self):
        self._x=(1,2)
    def __div__(self,div_fraction):
        return (self._x[0]*div_fraction[1],self._x[1]*div_fraction[0])

y=Test()
z=y/(1,3)
print(z)
Run Code Online (Sandbox Code Playgroud)

给我:

Traceback (most recent call last):
  File "E:/test.py", line 8, in <module>
   z=y/(1,3)
TypeError: unsupported operand type(s) for /: 'Test' and 'tuple'
Run Code Online (Sandbox Code Playgroud)

然而,当我改变__div__to __mul__和use *而不是/它做它应该做的事情.

我如何修复我得到的异常?

python tuples class operators divide

17
推荐指数
2
解决办法
8107
查看次数

Android Curved Listview

我在android中实现了一个listview,看起来就像这样并滚动 在此输入图像描述

我一直在通过在我的适配器的getView中设置膨胀行的布局参数来做到这一点,但是由此产生的问题是列表视图变得生涩.即,只有当一行完全从屏幕传递并且已调用getView时,才会更新行,并为所有可见行提供新的参数,从而导致可以应用动画,并且可以预先获取listview滚动位置.我想要的是视图之间的平滑过渡,即当列表视图向上移动时,内部视图应该与该移动精确地缩放.


这是我解决这个问题的第一级实现希望它可以帮助任何感兴趣的人,使用Bezier路径算法在活动开始时动态创建arrHeight和arrWidth

public class TimelinePinsAdapter extends ParentLazyAdapter {

    LinearLayout ll_PinsContainer;
    LinearLayout.LayoutParams llParams_PinParameters;

    public TimelinePinsAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
        super(a, d);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.rowsinflate, null, false);
        ll_PinsContainer = (LinearLayout) vi.findViewById(R.id.item);
        llParams_PinParameters =
            new LinearLayout.LayoutParams(0,
                (int) TimeLineActivity.arrHeight[position % 7]);
        ll_PinsContainer.setLayoutParams(llParams_PinParameters); …
Run Code Online (Sandbox Code Playgroud)

android listview android-animation

17
推荐指数
1
解决办法
1373
查看次数

是否可以在没有循环导入的模块中导入烧瓶配置值?

我正在使用带有蓝图的Flask来获取我的网站的骨架,我在使用我的应用程序深层配置类时遇到了问题.

这是一些虚拟代码,解释了我如何设置一切:

websiteconfig.py

class Config(object):
  pass

class ProductionConfig(Config):
  DEBUG = False

class DevelopmentConfig(Config):
  DEBUG = True
Run Code Online (Sandbox Code Playgroud)

website/__ init __.py:

# Some app code and config loading
app = Flask('website')
app.config.from_object('websiteconfig.DevelopmentConfig')

# Import some random blueprint
from website import users
app.register_blueprint(users.api)

# This works:
# print app.config['DEBUG']
Run Code Online (Sandbox Code Playgroud)

website/users/__ init __.py:

from flask import Blueprint
from website.users.models import test
api = Blueprint('users', __name__, url_prefix='/users')

# This works:
# print api.config['DEBUG']

# From models
print test()
Run Code Online (Sandbox Code Playgroud)

website/users/models.py:

# How can …
Run Code Online (Sandbox Code Playgroud)

python python-module circular-dependency flask

16
推荐指数
1
解决办法
5614
查看次数

PDB中的自动完成和Tab键

TAB在(pdb)提示符下,我一直试图做一些其他事情而不是插入标签.

我想到的是触发自动完成,例如在这里这里,但tab键除了向pdb添加选项卡之外没有做任何其他事情.

所以:

(pdb)var + tabKeyPressed

我想得到:

(pdb)variable

代替:

(pdb)var[          ]

python python-2.7 pdb

16
推荐指数
3
解决办法
4169
查看次数

如何重新安装lxml?

我在mac 10.7.5上使用python 2,7.5,beautifulsoup 4.2.1.我将使用lxml库解析xml页面,如beautifulsoup教程中所述.但是,当我运行我的代码时,它会显示出来

bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested:
lxml,xml. Do you need to install a parser library?
Run Code Online (Sandbox Code Playgroud)

我确信我已经通过所有方法安装了lxml:easy_install,pip,port等.我试着在我的代码中添加一行,看看是否安装了lxml:

import lxml
Run Code Online (Sandbox Code Playgroud)

然后python可以成功浏览此代码并再次显示上一条错误消息,发生在同一行.

所以我很确定已经安装了lxml,但没有正确安装.所以我决定卸载lxml,然后使用'正确'方法重新安装.但是当我输入时

easy_install -m  lxml
Run Code Online (Sandbox Code Playgroud)

表明:

Searching for lxml
Best match: lxml 3.2.1
Processing lxml-3.2.1-py2.7-macosx-10.6-intel.egg

Using /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/lxml-
3.2.1-py2.7-macosx-10.6-intel.egg

Because this distribution was installed --multi-version, before you can
import modules from this package in an application, you will need to
'import pkg_resources' and then use a 'require()' call similar to one of
these examples, …
Run Code Online (Sandbox Code Playgroud)

python lxml beautifulsoup easy-install

16
推荐指数
3
解决办法
3万
查看次数

使用readline读取txt文件python3

我一直在努力工作几个小时,我无法做到正确,任何帮助将不胜感激!我的问题是如何使用该函数.readline()读取文本文件的结尾?我知道这项.readlines()工作也是如此,但我正在尝试一次处理一行.

这是我到目前为止我的代码所拥有的:

    a = open("SampleTxt.txt","r")

    While True:

        a.readline()
Run Code Online (Sandbox Code Playgroud)

我的问题是,当我运行它时,我得到一个无限循环,一旦它不能再读取一行就不应该停止吗?

python readline python-3.x

16
推荐指数
1
解决办法
4万
查看次数

我在哪里可以找到python的内置类的方法和属性?

我试图找出python中所有异常类的母亲带来哪些方法和属性:Exception类.但是,由于官方文档似乎没有提供它,我遇到了一些麻烦.

我能找到的最好的是:http://docs.python.org/library/exceptions.html但只列出了内置的异常.

这是怎么回事?我已经习惯了Java和PHP文档,其中所有内容都放在桌子上:(

python documentation exception

15
推荐指数
2
解决办法
1万
查看次数