小编Tre*_*ton的帖子

Python除UnicodeError外?

在我的代码中,我不断收到此错误...

UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 390: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)

我试图除了UnicodeError和UnicodeEncodeError之外没有任何效果,问题是它是用户输入所以我无法控制它们放置的内容所以我需要所有编码错误来显示错误而不是崩溃程序的打印. .

try:
    argslistcheck = argslist[0]
    if argslistcheck[0:7] != "http://":
        argslist[0] = "http://" + argslist[0]
    with urllib.request.urlopen(argslist[0]) as url:
        source = url.read()
        source = str(source, "utf8")
    except urllib.error.URLError:
        print("Couln't connect")
        source = ""
    except UnicodeEncodeError:
        print("There was an error encrypting...")
        source = ""
Run Code Online (Sandbox Code Playgroud)

追溯:

Traceback (most recent call last):
  ..... things leading up to error
  File "C:\path", line 99, in grab print(source)
  File "C:\Python33\lib\encodings\cp437.py", line …
Run Code Online (Sandbox Code Playgroud)

python python-3.3

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

Python 3.3无法导入Crypt

当我在命令行输入导入Crypt时,它说:

>>>import crypt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\crypt.py", line 3, in <module>
    import _crypt
ImportError: No module named '_crypt'
Run Code Online (Sandbox Code Playgroud)

python python-3.3

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

Python通过括号分割字符串

不久前我问了一个问题(Python用空格和括号将未知字符串分割开),在我不得不改变思维方式之前,它一直很有效。我还没有掌握正则表达式,因此我需要一些帮助。

如果用户键入以下内容:

new test (test1 test2 test3) test "test5 test6"

我希望它看起来像这样的变量输出:

["new", "test", "test1 test2 test3", "test", "test5 test6"]

换句话说,如果一个单词用空格分隔,则将其与下一个单词分开;如果在括号中,则将括号中的整个单词组分开并删除它们。引号也是如此。

我目前正在使用的代码不符合上述标准(来自上面链接的答案):

>>>import re
>>>strs = "Hello (Test1 test2) (Hello1 hello2) other_stuff"
>>>[", ".join(x.split()) for x in re.split(r'[()]',strs) if x.strip()]
>>>['Hello', 'Test1, test2', 'Hello1, hello2', 'other_stuff']
Run Code Online (Sandbox Code Playgroud)

这很好用,但是如果有这样的话,那就是一个问题:

strs = "Hello Test (Test1 test2) (Hello1 hello2) other_stuff"

它将Hello和Test合并为一个拆分而不是两个拆分。

它还不允许同时使用括号和引号。

python string split

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

Python用空格和括号分割未知字符串

如果我有一个字符串可能是:

'Hello (Test1 test2) (Hello1 hello2) other_stuff'
Run Code Online (Sandbox Code Playgroud)

我想把它分成这样的东西:

split1='hello'
split2='Test1, test2'
split3='Hello1, hello2'
split4='other_stuff'
Run Code Online (Sandbox Code Playgroud)

然后我将把它放入一个变量中:

full_split=[split1, split2, split3, split4]
Run Code Online (Sandbox Code Playgroud)

而且,未知的字符串是,如果他们继续在末尾添加单词,它将继续添加分割变量(split5, split6

我正在研究正则表达式,但我不喜欢导入 python 不附带的模块。如果必须的话我会的。

python string split

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

C++使用模板定义结构

我不明白为什么我不能定义这个结构:

//Class.h

template <class T>
struct Callback {
    T* Object;
    std::function<void()> Function;
};

template <class T>
struct KeyCodeStruct {
    typedef std::unordered_map<SDL_Keycode, Callback<T>> KeyCode;
};

template <class T>
struct BindingStruct{
    typedef std::unordered_map<int, KeyCodeStruct<T>> Binding;
};


class Class {
public:
    template <class T>
    void bindInput(SDL_EventType eventType, SDL_Keycode key, Callback<T> f);
private:
    template <class T>
    BindingStruct<T> inputBindings; //How do I define this? This gives me an error.
}
Run Code Online (Sandbox Code Playgroud)

它给出了错误: Member 'inputBindings' declared as a template. 我不太了解模板所以我可能只是错过了我需要的信息.

更新(回应deviantfan)

现在我的cpp类遇到了我所拥有的函数的问题.

template <class T>
void …
Run Code Online (Sandbox Code Playgroud)

c++ templates struct

-1
推荐指数
1
解决办法
99
查看次数

标签 统计

python ×4

python-3.3 ×2

split ×2

string ×2

c++ ×1

struct ×1

templates ×1