您的位置:首页 > 编程语言 > Python开发

"list" in python

2014-01-20 16:49 399 查看
用python写了一个小脚本,用来去除文本文件中多余的空格,感觉python里字符串或数组的索引很有意思。

索引可正可负,也有类似于Matlab中的步长。特别注意的是,使用索引进行分片时,第一个索引是需要提取部分的第一个元素的索引值,而最后的索引则是分片之后右侧剩下部分的第一个元素的索引[1]。

# to remove the redundant space in each line

newFile = open("dst","w",encoding="UTF-8")
with open("src.txt",encoding="UTF-8") as f:
for line in f:
pos = line.index(' ')
n = len(line)
line1 = line[0:pos]
line2 = line[pos+1:n]
line = line1 + line2
newFile.write(line)
newFile.close()


Ref:

[1] /article/5461203.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: