使用正则表达式时获取TypeError

时间:2017-12-17 17:26:08

标签: python regex traceback

使用下面的代码,我不断收到错误。我有一个非常相似的示例代码,但这个代码似乎不起作用。此外,我确实通过regex101.com运行我的正则表达式,所以它应该工作。

Uri uriAudio = Uri.fromFile(new File(audioFilePath).getAbsoluteFile());
        final StorageReference filePath = ref.child("Education/image").child(uriImage.getLastPathSegment());
        final StorageReference audioRef = ref.child("Education/audio").child(uriAudio.getLastPathSegment());


                // on success upload audio
                audioRef.putFile(uriAudio).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(final UploadTask.TaskSnapshot audioSnapshot) {

                        //upload image
                        filePath.putFile(uriImage).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            @Override
                            public void onSuccess(final UploadTask.TaskSnapshot imageSnapshot) {

                                mProgress.dismiss();

                        @SuppressWarnings("VisibleForTests") Uri audioUrl= audioSnapshot.getDownloadUrl();
                        @SuppressWarnings("VisibleForTests") Uri imageUrl= imageSnapshot.getDownloadUrl();

代码:

Traceback (most recent call last):
  File "/Users/name/Assignment 3.py", line 8, in <module>
    print(re.match(pattern, file1))
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/re.py", line 137, in match
    return _compile(pattern, flags).match(string)
TypeError: expected string or buffer    

1 个答案:

答案 0 :(得分:1)

我猜你真正想要的是查看文件的内容

with open("10000DirtyNames.csv", "r") as file1:
    if (re.search(pattern, file1.read()):
        print("Match")
    else:
        print("Does not match")

此外,re.search()re.match()之间存在差异,后者仅在字符串的开头起作用(因此,隐式设置了锚点)。
最后,可以通过None或仅使用is not None

来检查if x: