简体   繁体   English

如何编辑此替换代码以仅替换 n 行

[英]How do I edit this replacement code to only replace n line

the code I am working with essentially is supposed to go into a file ('Coincount.txt') and for nth line, if in its 4th portion (hence the 3) the string is equal to the variable named 'target', replace the fourth portion with the replacement.我正在使用的代码基本上应该将 go 放入文件('Coincount.txt')中,对于第 n 行,如果在其第 4 部分(因此是 3)中,字符串等于名为 'target' 的变量,请替换第四部分与替换。

import fileinput
if number_of_bags.is_integer():
    target, replacement = 'N', 'Y'
else:
    target, replacement = 'Y', 'N'

with fileinput.FileInput('CoinCount.txt', inplace=True) as fileobj:
    for n,line in enumerate(fileobj):
        words = line.rstrip().split(',')
        if words[3] == target:
            words[3] = replacement
        print(','.join(words))

    f = fileobj.lineno()  # Number of lines processed.
i = i + 1

print(f'Done, {f} lines processed')

also please know that the txt file is designed like such:也请知道 txt 文件是这样设计的:

Abena,5p,325.00,Y
Malcolm,1p,3356.00,Y
Jane,£2,120.00,Y
Andy,£1,166.25,N
Sandip,50p,160.00,Y
Liz,20p,250.00,Y
Andy,20p,250.00,Y
Andy,50p,160.00,Y
Jane,£1,183.75,N

the 4th portion represents the 'Y' or 'N'第 4 部分代表“Y”或“N”

this is also apart of a bigger function but all that you need to know is that number_of_bags is a variable to the line that is being scanned which is how this thing works.这也是更大的 function 的一部分,但您需要知道的是 number_of_bags 是正在扫描的行的变量,这就是这件事的工作原理。

Please tell me how I need to restructure this code or implement a new line within it so that it works properly.请告诉我我需要如何重组此代码或在其中实现一个新行以使其正常工作。

at the moment if I input this, the output in the txt file is:目前如果我输入这个,txt文件中的output是:

Abena,5p,325.00,Y
Malcolm,1p,3356.00,Y
Jane,£2,120.00,Y
Andy,£1,166.25,Y
Sandip,50p,160.00,Y
Liz,20p,250.00,Y
Andy,20p,250.00,Y

this happens due to the .join as it replaces all of the 4th portions of the lines to replacement .这是由于.join因为它替换了行的所有第 4 部分来replacement的。 I want to know how I can make this function only target nth line so that every line will either replace the 4th portion or will just leave it if it is already input correctly.我想知道如何使这个 function 仅针对第 n 行,以便每一行都将替换第 4 部分,或者如果已经正确输入,则将其保留。

if i get your question right you want to only change one line that, so lets say you have this:如果我的问题正确,您只想更改一行,那么可以说您有这个:

line_to_replace = 3 # var that will incidate what line you want to change

so you can change your code:所以你可以改变你的代码:

for n,line in enumerate(fileobj):

into:进入:

for n,line in enumerate(fileobj):
    if n == line_to_replace: # simple if to check the current line number
        ...code...

暂无
暂无

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

相关问题 如何替换字符串中的 `\n` 而不是 Python 中的 `\n\n`? - How do I replace `\n` in a string but not `\n\n` in Python? 如果要替换的行在我将获得替换值的行上方,则如何从.txt中的一行替换为另一行? - How to .replace from a line to another in a .txt, if the line to be replaced is above the line where i'll get the value for replacement? 如何让这个 Python 代码只输出一行? - How do I get this Python code to output only one line? 如何在Python中仅打印一行? “ \\ n”没有给我想要的效果 - How do I print only one new line in Python? “\n” does not give me my desired effect 在Python中,如何在一行代码中创建一个包含n个字符的字符串? - In Python, how do I create a string of n characters in one line of code? 如何替换 Python 中的一行 - How do I replace a line in Python 如何编辑此算法以便创建组合而无需替换或重用元素? - How do I edit this algorithm so that it creates combinations without replacement or reusing an element? 如何用替换字符替换列表中字符串中的所有特定字符? - How do I replace all of the specific characters in the string from the lists with the replacement characters? 如何在Python中编辑一行(改写和替换) - How to edit a line (overwright and replace) in Python 从行中删除/ n,同时在Python中的该行中进行替换 - Strip /n from line and simultaneously do a replace in that line in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM