![](/img/trans.png)
[英]how to count characters in a string without the white spaces using python?
[英]How to find a specific characters in string without spaces in python?
嘿,我有讀取文件和處理它們的循環。 我想從這個循環中排除一些文件。
假設文件名是:(它們都在同一個名為“data”的目錄中)
'123abc456.csv'
'123bca456.csv'
'455abc324.csv'
我想排除名稱中包含“bca”部分的文件。 我不想讀它們。
我該怎么做?
for file in glob.glob("path/data/*csv"):
if "bca" in file:
None
else:
df = df.read_csv(file)
不管用。 我也收到“bca”文件。
謝謝!
In [183]: filenames
Out[183]: ['123abc456.csv', '123bca456.csv', '455abc324.csb']
In [184]: [x for x in filenames if 'bca' not in x]
Out[184]: ['123abc456.csv', '455abc324.csb']
您可以使用 not like 這樣來否定表達式:
假設您的路徑是“/home/user/Documents/*”
import glob
for file_path in glob.glob("/home/user/Documents/*"):
if not "bca" in file:
print(file_path)
# do something with file
嗯,你可以試試
for file in glob.glob("path/data/*csv"):
if "bca" not in file:
df = df.read_csv(file)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.