Hướng dẫn check if string in filename python - kiểm tra xem chuỗi trong tên tệp python

Tôi đang cố gắng tìm một cách mà tên của tệp mà chương trình đang đọc sẽ được kiểm tra nếu nó chứa bất kỳ chuỗi nào như dưới đây. Tôi không chắc đó có phải là cách đúng đắn để đi về nó không. Chuỗi sẽ là một biến toàn cầu vì tôi phải sử dụng nó sau này trong chương trình

class Wordnet():

    def __init__(self):
        self.graph = Graph()
        self.filename = ''
        self.word_type = ''

    def process_file(self):
        self.filename = "noun.txt"
        self.file = open(self.filename, "r")
        return self.file, self.filename

    def check_word_type(self, filename):
        if 'noun' in filename:
            self.word_type = 'noun'
        elif 'verb' in filename:
            self.word_type = 'verb'
        elif 'vrb' in filename:
            self.word_type = 'verb'
        elif adj in filename:
            self.word_type = 'adj'
        elif adv in filename:
            self.word_type = 'adv'
        else:
            self.word_type = ''
        return self.word_type

if __name__ == '__main__':
    wordnet = Wordnet()
    my_file = wordnet.process_file()  
    print wordnet.word_type

Bất kỳ sự trợ giúp nào đều sẽ là tuyệt vời

Đã hỏi ngày 4 tháng 7 năm 2013 lúc 14:42Jul 4, 2013 at 14:42

Hướng dẫn check if string in filename python - kiểm tra xem chuỗi trong tên tệp python

JohnconneelyjohnconneelyJohnConneely

1.3334 huy hiệu vàng16 Huy hiệu bạc29 Huy hiệu đồng4 gold badges16 silver badges29 bronze badges

Thử cái này:

def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type

Demetris

2.7722 huy hiệu vàng23 Huy hiệu bạc32 Huy hiệu đồng2 gold badges23 silver badges32 bronze badges

Đã trả lời ngày 4 tháng 7 năm 2013 lúc 14:45Jul 4, 2013 at 14:45

0

Bạn không gọi check_word_type() ở bất cứ đâu. Thử:

def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename

Đã trả lời ngày 4 tháng 7 năm 2013 lúc 14:45Jul 4, 2013 at 14:45

Hướng dẫn check if string in filename python - kiểm tra xem chuỗi trong tên tệp python

Bạn không gọi check_word_type() ở bất cứ đâu. Thử:mishik

Mishikmishik9 gold badges42 silver badges66 bronze badges

Mã nguồn: lib/fnmatch.py Lib/fnmatch.py


Mô-đun này cung cấp hỗ trợ cho các ký tự đại diện kiểu vỏ Unix, không giống như các biểu thức thông thường (được ghi lại trong mô-đun re). Các nhân vật đặc biệt được sử dụng trong các ký tự đại diện kiểu vỏ là:

Họa tiết

Nghĩa

*

Phù hợp với mọi thứ

?

khớp với bất kỳ ký tự đơn lẻ

[seq]

khớp với bất kỳ ký tự nào trong SEQ

def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
0

khớp với bất kỳ ký tự nào không có trong seq

Đối với một trận đấu theo nghĩa đen, hãy bọc các nhân vật tổng hợp trong ngoặc. Ví dụ,

def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
1 khớp với ký tự
def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
2.

Lưu ý rằng bộ phân cách tệp (

def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
3 trên UNIX) không đặc biệt đối với mô -đun này. Xem mô -đun
def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
4 để mở rộng tên đường dẫn (
def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
4 sử dụng
def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
6 để phù hợp với các phân đoạn PathName). Tương tự, tên tệp bắt đầu với một khoảng thời gian không đặc biệt cho mô -đun này và được khớp với các mẫu *?.

Cũng lưu ý rằng

def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
9 với mức tối đa của 32768 được sử dụng để lưu trữ các mẫu regex được biên dịch trong các chức năng sau:
def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
0,
def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
1,
def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
6.

fnmatch.fnmatch (tên tệp, mẫu) ¶fnmatch(filename, pattern)

Kiểm tra xem chuỗi tên tệp có khớp với chuỗi mẫu hay không, trả về

def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
3 hoặc
def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
4. Cả hai tham số đều được chuẩn hóa trường hợp bằng cách sử dụng
def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
5.
def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
1 có thể được sử dụng để thực hiện so sánh nhạy cảm trường hợp, bất kể tiêu chuẩn đó cho hệ điều hành.

Ví dụ này sẽ in tất cả các tên tệp trong thư mục hiện tại với phần mở rộng

def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
7:

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print(file)

fnmatch.fnmatchcase (tên tệp, mẫu) ¶fnmatchcase(filename, pattern)

Kiểm tra xem tên tệp có khớp với mẫu hay không, trả về

def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
3 hoặc
def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
4; Việc so sánh là nhạy cảm trường hợp và không áp dụng
def process_file(self):
    self.filename = "noun.txt"
    self.file = open(self.filename, "r")
    self.check_word_type(self, self.filename)
    return self.file, self.filename
5.

fnmatch.filter (tên, mẫu) ¶filter(names, pattern)

Xây dựng một danh sách từ các yếu tố của các tên có thể khớp với mẫu. Nó giống như

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print(file)
1, nhưng đã thực hiện hiệu quả hơn.

fnmatch.translate (mẫu) ¶translate(pattern)

Trả lại mẫu kiểu vỏ được chuyển đổi thành một biểu thức chính quy để sử dụng với

import fnmatch
import os

for file in os.listdir('.'):
    if fnmatch.fnmatch(file, '*.txt'):
        print(file)
2.

Example:

>>> import fnmatch, re
>>>
>>> regex = fnmatch.translate('*.txt')
>>> regex
'(?s:.*\\.txt)\\Z'
>>> reobj = re.compile(regex)
>>> reobj.match('foobar.txt')
<re.Match object; span=(0, 10), match='foobar.txt'>

Xem thêm

Mô -đun
def check_word_type(self, filename):
    words = ['noun','verb','vrb','adj','adv'] #I am not sure if adj and adv are variables
    self.word_type = ''
    for i in words:
        if i in filename:
            self.word_type = str(i) #just make sure its string

    return self.word_type
4

Mở rộng đường dẫn kiểu vỏ Unix.

Làm cách nào để kiểm tra xem một chuỗi có chứa trong Python không?

Cách dễ nhất và hiệu quả nhất để xem liệu một chuỗi có chứa chuỗi con hay không bằng cách sử dụng nếu ... trong các câu lệnh, sẽ trả về đúng nếu phát hiện nền tảng.Ngoài ra, bằng cách sử dụng hàm find (), có thể nhận được chỉ mục rằng một chuỗi con bắt đầu tại hoặc -1 nếu Python không thể tìm thấy chuỗi con.using if ... in statements, which return True if the substring is detected. Alternatively, by using the find() function, it's possible to get the index that a substring starts at, or -1 if Python can't find the substring.

Làm thế nào để bạn tìm thấy tên tệp trong Python?

Chương trình Python để lấy tên tệp từ đường dẫn tệp..
Nhập hệ điều hành # Tên tệp với phần mở rộng file_name = os.path.basename ('/root/file.ext') # Tên tệp không có phần mở rộng in (os.path.splitext (file_name) [0]) Chạy mã ..
Nhập bản in hệ điều hành (Os.Path.SpliteXt (File_Name)) ....
từ Pathlib Nhập đường dẫn PRINT (đường dẫn ('/root/file.ext'). Thân cây).

Làm thế nào để bạn sử dụng chứa trong Python?

Phương thức __Contains __ () là phương pháp của lớp chuỗi Python có thể được sử dụng để kiểm tra xem lớp có chứa chuỗi khác hay không ...
My_String = 'Chào mừng'.
in ("Chuỗi của tôi chứa \ 'w \' =", my_string. ....
print ("Chuỗi của tôi chứa \ 'w \' =", my_string ..