如何从Python中的字符串中删除前导和尾随空格?
例如:
" Hello " --> "Hello"
" Hello" --> "Hello"
"Hello " --> "Hello"
"Bob has a cat" --> "Bob has a cat"
Run Code Online (Sandbox Code Playgroud) 如何删除python字符串中的所有空格?例如,我要一个字符串喜欢strip my spaces被变成stripmyspaces,但我似乎无法做到与strip():
>>> 'strip my spaces'.strip()
'strip my spaces'
Run Code Online (Sandbox Code Playgroud) 我有一个以多个空格开头的文本字符串,在2和4之间变化.
删除前导空格的最简单方法是什么?(即删除某个角色之前的所有内容?)
" Example" -> "Example"
" Example " -> "Example "
" Example" -> "Example"
Run Code Online (Sandbox Code Playgroud) 基本上,我要求用户在控制台中输入一串文本,但字符串很长并包含许多换行符.如何获取用户的字符串并删除所有换行符以使其成为单行文本.我获取字符串的方法非常简单.
string = raw_input("Please enter string: ")
Run Code Online (Sandbox Code Playgroud)
有没有不同的方法我应该从用户那里抓取字符串?我在Mac上运行Python 2.7.4.
PS显然我是一个菜鸟,所以即使解决方案不是最有效的,使用最简单语法的解决方案也会受到赞赏.
我试图在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)
这似乎是一件简单的事情,但我在这里缺少一些东西.我应该进口什么吗?
任何人都可以解释为什么下面的示例1有效,何时r不使用前缀?我认为r只要使用转义序列,就必须使用前缀.示例2和示例3证明了这一点.
# example 1
import re
print (re.sub('\s+', ' ', 'hello there there'))
# prints 'hello there there' - not expected as r prefix is not used
# example 2
import re
print (re.sub(r'(\b\w+)(\s+\1\b)+', r'\1', 'hello there there'))
# prints 'hello there' - as expected as r prefix is used
# example 3
import re
print (re.sub('(\b\w+)(\s+\1\b)+', '\1', 'hello there there'))
# prints 'hello there there' - as expected as r prefix is not used
Run Code Online (Sandbox Code Playgroud) subject_dic = {}
inputFile = open(filename)
for line in inputFile:
split_line = string.split(line, ',')
subject_dic[split_line[0]] = tuple(split_line[1:3])
print subject_dic
Run Code Online (Sandbox Code Playgroud)
我越来越
{'6.08': ('1', '10\n'), '6.09': ('3', '7\n'), '6.19': ('8', '19'), '6.10': ('8', '18\n'), '6.00': ('10', '1\n'), '6.01': ('5', '4\n'), '6.02': ('5', '6\n'), '6.03': ('2', '9\n'), '6.04': ('1', '2\n'), '6.05': ('1', '18\n'), '6.06': ('5', '19\n'), '6.07': ('2', '10\n'), '6.13': ('9', '16\n'), '6.18': ('10', '4\n'), '6.15': ('10', '6\n'), '6.16': ('6', '9\n'), '6.12': ('6', '3\n'), '6.17': ('9', '3\n'), '6.14': ('10', '8\n'), '6.11': ('6', '8\n')} …Run Code Online (Sandbox Code Playgroud) 我正在使用scrapy构建数据提取,并希望规范化从HTML文档中提取的原始字符串.这是一个示例字符串:
Sapphire RX460 OC 2/4GB
Run Code Online (Sandbox Code Playgroud)
发现两个组的两个空格前述字符串文字之间的OC和2.
Python提供修剪,如我如何用Python修剪空白?但是,这不会处理两者之间的空间OC和2,我需要合并为一个空间.
我尝试使用normalize-space()XPath,同时使用我的scrapy Selector提取数据,但是它的工作方式很复杂,并且具有强大的向右漂移:
product_title = product.css('h3').xpath('normalize-space((text()))').extract_first()
Run Code Online (Sandbox Code Playgroud)
有没有一种使用Python规范化空格的优雅方法?如果不是单行,有没有办法可以将上面的行分解为更容易阅读的内容而不会出现缩进错误,例如
product_title = product.css('h3')
.xpath('normalize-space((text()))')
.extract_first()
Run Code Online (Sandbox Code Playgroud) 嗨,想删除 csv 文件中的前导和尾随空格
24333, 116, 47,MCD,00000000000000000017996, 112
24333, 116, 47,MCD,00000000000000610036485, 112
24333, 116, 47,MCD,00000000000000610036485, 112
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我尝试的代码
import csv
csvfile= open('strip.csv','r')
csvfile1= open('strip11.csv','w')
stripped = (row.strip() for row in csvfile)
reader = csv.reader(stripped,delimiter=' ')
writer= csv.writer(csvfile1)
for row in reader:
writer.writerow(row)
Run Code Online (Sandbox Code Playgroud) 我是Python的新手,我有一个字符串拆分问题,我需要帮助
input = "\"filename.txt\", 1234,8973,\"Some Description \""
Run Code Online (Sandbox Code Playgroud)
input 包含字符串和数字,并且可能存在前导和尾随空格的情况
预期产量应该是
['filename.txt', '1234', '8973', 'Some Description']
Run Code Online (Sandbox Code Playgroud)
可以拆分完成工作还是需要正则表达式?
如何将字符串转换为必需的字符串(ans)?这是python中的简单字符串操作.
s = '(107, 163, 138, 255)'
ans = '(107,163,138,255)'
Run Code Online (Sandbox Code Playgroud) data = " Taylors Lakes, VIC "
Run Code Online (Sandbox Code Playgroud)
我需要正则表达式从这个字符串中删除多余的空格,而不是从单词之间删除多余的空格,以便字符串看起来像这样
数据 = "泰勒湖,维多利亚州"