我是python的新手,现在我没有主意了。 我要尝试的是:我有一个文件
示例:
254 578 name1 *--21->28--* secname1
854 548 name2 *--21->28--* secname2
944 785 name3 *--21->28--* secname3
1025 654 name4 *--21->28--* secname4
这些文件之间的有很多空格,我不想为每一行删除“ name *”和“ secname *”之间的特定空格。我不知道该怎么做,如示例所示,删除字符/空格21-> 28
我到目前为止所得到的:
fobj_in = open("85488_66325_R85V54.txt")
fobj_out = open("85488_66325_R85V54.txt","w")
for line in fobj_in:
fobj_in.close()
fobj_out.close()
最后它应该看起来像:
254 578 name1 secname1
854 548 name2 secname2
944 785 name3 secname3
1025 654 name4 secname4
答案 0 :(得分:3)
要按特定索引位置删除字符,您必须使用切片
for line in open('85488_66325_R85V54.txt'):
newline = line[:21] + line[29:]
print(newline)
删除第21:28列中的字符(示例中的所有空格)
答案 1 :(得分:2)
只需拆分行,然后弹出不需要的元素即可。
fobj_in = open('85488_66325_R85V54','r')
fobj_out = open('85488_66325_R85V54.txt', 'a')
for line in fobj_in:
items = line.split()
items.pop(3)
fobj_out.write(' '.join(items)+'\n')
fobj_in.close()
fobj_out.close()
答案 2 :(得分:1)
您可以仅使用字符串对象的split
方法,如下所示:
f = open('my_file.txt', 'r')
data = f.readlines()
final_data = []
for line in data:
bits = line.split()
final_data.append([bits[0], bits[1], bits[2], bits[4]])
基本上,我只是在说明如何使用split
方法将每一行分成单独的块,在这一点上,您可以执行所需的任何操作,例如打印所有这些位并有选择地丢弃其中一列
答案 3 :(得分:1)
我可以建议一种可靠的方法来校正输入线。
#!/usr/bin/env ipython
# -----------------------------------
line='254 578 name1 *--21->28--* secname1';
# -----------------------------------
def correctline(line,marker='*'):
status=0;
lineout='';
for val in line:
if val=='*':
status=abs(status-1);continue
if status==0:
lineout=lineout+val;
elif status == 1:
lineout=lineout
# -----------------------------------
while lineout.__contains__(' '):
lineout=lineout.replace(' ',' ');
return lineout
# ------------------------------------
print correctline(line)
基本上,它循环遍历输入文件的元素。当它找到要从其开始跳过文本的标记时,它会跳过它,最后只是用一个空格替换太多空格。
答案 4 :(得分:1)
如果名称长短不一,并且您不想删除它们之间的一定数量的空格,则可以搜索空白字符以查找sname
到name
的结尾:
# open file in "read" mode
fobj_in = open("85488_66325_R85V54.txt", "r")
# use readlines to create a list, each member containing a line of 85488_66325_R85V54.txt
lines = fobj_in.readlines()
# For each line search from the end backwards for the first " " char
# when this char is found create first_name which is a list containing the
# elements of line from here onwards and a second list which is the elements up to
# this point. Then search for a non " " char and remove the blank spaces.
# remaining_line and first_name can then be concatenated back together using
# + with the desired number of spaces between then (in this case 12).
for line_number, line in enumerate(lines):
first_name_found = False
new_line_created = False
for i in range(len(line)):
if(line[-i] is " " and first_name_found is False):
first_name = line[-i+1:]
remaining_line = line[:-i+1]
first_name_found = True
for j in range(len(remaining_line)):
if(remaining_line[-j-1] is not " " and new_line_created == False):
new_line = remaining_line[0:-j]+ " "*12 + first_name
new_line_created = True
lines[line_number] = new_line
然后将lines
写到85488_66325_R85V54.txt
。
答案 5 :(得分:-1)
您可以尝试如下操作:
for line in fobj_in:
setstring = line
print(setstring.replace(" ", "")