gpt4 book ai didi

Python 无法从返回的字符串中删除空格

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:40 25 4
gpt4 key购买 nike

我已经尝试过.replace,我已经尝试过trim,我已经尝试了几乎所有的解决方案来让这些该死的空白让我一个人呆着,但它们不会消失。

import win32api
import string
import re

disks = win32api.GetLogicalDriveStrings()
disks = disks.translate({ord(c): None for c in ':\\ '})

disks = re.sub(r'\s+','',disks, flags=re.UNICODE)

print (disks)

for disk in disks.strip().split(): # or just disks without the other stuff
print('Trying drive: '+disk)
try:
drives,_,_,_,_ = win32api.GetVolumeInformation(disk+':\\')
print ('Got info for drive: ' + disk)
print(drives)
except Exception as details:
print('Ooops, not a hard drive')

不带.strip().split()的输出

== RESTART: C:\~~~~~~ ==
C D E F
Trying drive: C
Got info for drive: C
Windows
Trying drive:
Ooops, not a hard drive
Trying drive: D
Ooops, not a hard drive
Trying drive:
Ooops, not a hard drive
Trying drive: E
Got info for drive: E
Media 2
Trying drive:
Ooops, not a hard drive
Trying drive: F
Ooops, not a hard drive
Trying drive:
Ooops, not a hard drive

不带.strip().split()的输出

== RESTART: C:\~~~~ ==
C D E F
Trying drive: C D E F
Ooops, not a hard drive

现在已经尝试

" ".join(disks.split())

最佳答案

您可以在 split 本身中包含所需的分隔符。像这样的东西:

import win32api

disks = win32api.GetLogicalDriveStrings()
disks = disks.split(":\\\x00")

for disk in disks:
if len(disk):
print('Trying drive: '+disk)
try:
drives = win32api.GetVolumeInformation(disk+':\\')
print ('Got info for drive: ' + disk)
print(drives)
except Exception as details:
print('Ooops, not a hard drive')

对我来说,这会产生类似的结果:

Trying drive: C
Got info for drive: C
('OS', -1470383266, 255, 65470719, 'NTFS')
Trying drive: D
Ooops, not a hard drive
Trying drive: G
Ooops, not a hard drive
Trying drive: H
Ooops, not a hard drive
Trying drive: W
Got info for drive: W
('Offline', 0, 255, 6, 'CSC-CACHE')

关于Python 无法从返回的字符串中删除空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43960061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com