是否有一个Python函数可以从字符串中修剪空格(空格和制表符)?
示例:\t example string\t
→example string
我有一些python代码分裂逗号,但不剥离空格:
>>> string = "blah, lots , of , spaces, here "
>>> mylist = string.split(',')
>>> print mylist
['blah', ' lots ', ' of ', ' spaces', ' here ']
Run Code Online (Sandbox Code Playgroud)
我宁愿最终删除这样的空格:
['blah', 'lots', 'of', 'spaces', 'here']
Run Code Online (Sandbox Code Playgroud)
我知道我可以遍历列表和strip()每个项目,但是,因为这是Python,我猜测有更快,更简单,更优雅的方式.
什么是一个干净,高效的JavaScript实现来从字符串中去除前导和尾随空格?
例如:
" dog"
"dog "
" dog "
" dog "
一切都变成了
"dog"
如何删除python字符串中的所有空格?例如,我要一个字符串喜欢strip my spaces
被变成stripmyspaces
,但我似乎无法做到与strip()
:
>>> 'strip my spaces'.strip()
'strip my spaces'
Run Code Online (Sandbox Code Playgroud) find . -type f -print
Run Code Online (Sandbox Code Playgroud)
打印出来
./file1
./file2
./file3
Run Code Online (Sandbox Code Playgroud)
任何方式使其打印
file1
file2
file3
Run Code Online (Sandbox Code Playgroud)
?
我必须在表格中列出大量的单词:
['this\n', 'is\n', 'a\n', 'list\n', 'of\n', 'words\n']
Run Code Online (Sandbox Code Playgroud)
然后使用条带功能,将其转换为:
['this', 'is', 'a', 'list', 'of', 'words']
Run Code Online (Sandbox Code Playgroud)
我以为我写的东西会起作用,但我一直都会收到错误说:
"'list'对象没有属性'strip'"
这是我试过的代码:
strip_list = []
for lengths in range(1,20):
strip_list.append(0) #longest word in the text file is 20 characters long
for a in lines:
strip_list.append(lines[a].strip())
Run Code Online (Sandbox Code Playgroud) 我需要严格优化可执行文件的大小(ARM
开发),我注意到在我当前的构建方案(gcc
+ ld
)中,未使用的符号不会被剥离.
的用法arm-strip --strip-unneeded
为生成的可执行文件/库不改变可执行文件的输出大小(我不知道为什么,也许它根本不能).
修改我的构建管道的方式是什么(如果存在),以便从结果文件中删除未使用的符号?
我甚至都不会想到这一点,但我当前的嵌入式环境并不是非常"强大",甚至500K
可以节省2M
非常好的加载性能.
更新:
不幸的是,gcc
我使用的当前版本没有-dead-strip
选项,并且-ffunction-sections... + --gc-sections
for ld
不会对结果输出产生任何显着差异.
我很震惊,这甚至成了问题,因为我确信gcc + ld
应该自动删除未使用的符号(为什么他们甚至要保留它们?).
我想从中删除双引号
string = '"" " " ""\\1" " "" ""'
Run Code Online (Sandbox Code Playgroud)
成为
string = '" " " ""\\1" " "" "'
Run Code Online (Sandbox Code Playgroud)
我试图用rstrip
,lstrip
并strip('[^\"]|[\"$]')
但没有奏效.
我怎样才能做到这一点?感谢你们对我的帮助.
在其他更改中,JDK 11为java.lang.String类引入了6种新方法:
repeat(int)
- 重复String的次数与int
参数提供的次数相同lines()
- 使用Spliterator延迟提供源字符串中的行isBlank()
- 指示String是否为空或仅包含空格字符stripLeading()
- 从头开始删除空白区域stripTrailing()
- 从末端移除空白区域strip()
- 从字符串的开头和结尾删除空白区域 特别是,strip()
看起来非常相似trim()
.根据这篇文章, strip*()
方法旨在:
String.strip(),String.stripLeading()和String.stripTrailing()方法修剪目标字符串的正面,背面或正面和背面的空白[由Character.isWhiteSpace()确定].
String.trim()
JavaDoc声明:
/**
* Returns a string whose value is this string, with any leading and trailing
* whitespace removed.
* ...
*/
Run Code Online (Sandbox Code Playgroud)
这几乎与上面的引用相同.
究竟之间的差别String.trim()
,并String.strip()
因为Java的11?
我试图在Linux上删除python 2.7中的所有空格/制表符/换行符.
我写了这个,应该做的工作:
myString="I want to Remove all white \t spaces, new lines \n and tabs \t"
myString = myString.strip(' \n\t')
print myString
Run Code Online (Sandbox Code Playgroud)
输出:
I want to Remove all white spaces, new lines
and tabs
Run Code Online (Sandbox Code Playgroud)
这似乎是一件简单的事情,但我在这里缺少一些东西.我应该进口什么吗?
strip ×10
python ×6
string ×4
trim ×3
whitespace ×2
c ×1
c++ ×1
find ×1
gcc ×1
java ×1
java-11 ×1
javascript ×1
ld ×1
list ×1
python-2.7 ×1
python-3.x ×1
spaces ×1
unix ×1