简体   繁体   English

如何在文本文件的特定列位置添加或替换某些字符串

[英]How to add or replace some string at a particular column position in a text file

How to add or replace some string at a particular column position in a text file: for example i have one sentence in a particular file example given below: 如何在文本文件的特定列位置添加或替换某些字符串:例如,在下面给出的特定文件示例中,我有一个句子:

Roxila almost lost
Roxila almost lost
Roxila almost lost
Roxila almost lost
Roxila almost lost

"enumerate()" gives some thing like this “ enumerate()”给出了类似的东西

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
R o x i l a   a l m  o  s  t     l  o  s  t 

now i want to mutate index "6" which is a "space" with "*" at each row. 现在我想突变索引“ 6”,它是每行带有“ *”的“空格”。 like this: 像这样:

Roxila*almost lost

how can i do this with python. 我该如何用python做到这一点。 Please help 请帮忙

You can use slicing to get a new string and fileinput module to update the existing file: 您可以使用切片来获取新的字符串,然后使用fileinput模块来更新现有文件:

Slicing demo: 切片演示:

>>> s = "Roxila almost lost"
'Roxila almost lost'
>>> s [:6] + '*' + s[7:]
'Roxila*almost lost'

Updating the file: 更新文件:

import fileinput
for line in fileinput.input('foo.txt', inplace=True):
    print line[:6] + '*' + line[7:],
for line in f:
   line = line.rstrip()
   newline = line[:6] + '*' + line[7:]
   print newline

Another approach, using replace 另一种方法,使用替换

with open("yourfile.txt", "r") as file:
    lines = file.read().split("\n")
    newlines = []
    for line in lines:
        newline = line.replace(" ", "*", 1)
        newlines.append(newline)

with open("newfile.txt", "w") as newfile:    
    newfile.write("\n".join(newlines))

If your first string change, which means the length, in that case slicing won't work: 如果您的第一个字符串改变了,这意味着长度,在这种情况下切片将不起作用:

Better to use this way: 最好使用这种方式:

>>> s.split(' ')
['Roxila', 'almost', 'lost']
>>> p = s.split(' ')
>>> p[0]+'*'+' '.join(p[1:])
'Roxila*almost lost'
>>>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python在csv中的特定列中逐行添加一些文本 - How to Add some text in row by row in particular column in csv using python 检查 dataframe 的列中是否存在某些特定字符串; 如果存在,则为其添加前缀 - Checking if some particular string exists in a column of a dataframe or not ; if exists then add a prefix to it Python 如何替换文本文件中特定行中的特定单词? - Python How to replace a particular word in a particular line in a text file? 用特定字符串替换列值 - Replace the column value with a particular string 如何更改文本文件中的特定字符串? - How to change a particular string in a text file? 使用 python 将文本添加到现有 csv 文件的特定列的每个单元格 - Add text to each cell of a particular column of an existing csv file with python 使用python将文本添加到现有excel文件的特定列的每个单元格中 - Add text to each cell of a particular column of an existing excel file with python 如何用除某些值之外的字符串替换 Pandas 列的字符串值? - How to replace string values of a Pandas column with a string except some values? 如何在不删除原始内容的情况下在文本文件中的特定位置写入? - How to write at a particular position in text file without erasing original contents? 如何用星号替换字符串中某些被禁词的字符? 禁止的单词存储在另一个文本文件中 - How to replace character of some banned word of a string with star? banned words are stored in another text file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM