擅长:python、mysql、java
<p>在pythonv3.x中</p>
<pre><code>print(word, end='')
</code></pre>
<p>为了避免出现新的情况。你知道吗</p>
<p>在pythonv2.x中</p>
<pre><code>print word,
</code></pre>
<p>在打印的项目末尾使用逗号。请注意,与v3不同的是,在连续打印之间会有一个<em>空格</em></p>
<p>注意,<code>print(word),</code>不会阻止v3.x中的换行</p>
<p>---</p>
<p><em><strong>更新</strong>基于原始帖子中的编辑重新编码问题:</em></p>
<p>有输入:</p>
<pre><code>i li4ke l0ke like p0tatoes potatoes
300 bla-bla-bla 00bla-bla-0211
</code></pre>
<p>此代码:</p>
<pre><code>def hasDigit(w):
for c in w:
if c.isdigit():
return True
return False
with open("data.txt") as f:
for line in f:
digits = [w for w in line.split() if not hasDigit(w)]
if digits:
print ' '.join(digits)
# break # uncomment the "break" if you ONLY want to process the first line
</code></pre>
<p>将产生所有不包含数字的“单词”:</p>
<pre><code>i like potatoes
bla-bla-bla <-- this line won't show if the "break" is uncommented above
</code></pre>
<p>注:</p>
<p>如果您只想处理<em>文件的第一行,<strong>或者如果问题是您的脚本只处理了<em>文件的第一行,那么post有点不清楚。此解决方案可以以任何方式工作,这取决于<code>break</code>语句是否被注释掉。你知道吗</p>