简体   繁体   English

使用Python编辑文件中的单词

[英]Editing words in a file using Python

I'm not getting the logic to edit a file in python which has following content: 我没有逻辑来编辑python中具有以下内容的文件:

session1: Part1
     host1:           #All the four host here should be user input 
     host2:
     host3:
     host4:
end

session2: Part2    #The UP,DOWN,LEFT,RIGHT values can be jumbled among sides                                            
 host1:
    up = host2  
 host2:
    down = host3
 host3:
    left = host4
 host4:
    right = host3

end

In first part of the file "session1" it should get either 2 or 3 or 4 inputs for host 1 to 4 as a string from user, and in second part of the file "session2" it has to update the same input accordingly in place of "host1 to 4:". 在文件“ session1”的第一部分中,应该从用户那里获取主机1至4的2或3或4输入作为字符串,而在文件“ session2”的第二部分中,它必须相应地更新相同的输入“主机1至4:”。 But the sides (up,left,down,right) can be jumbled according to the user, means they can move the value of up to down or down to right or left but the "host1,2,3,4:" in "session2"should remains same. 但是根据用户的不同,侧面(上,左,下,右)可以杂乱无章,这意味着它们可以将up的值上下或左右或左右移动,但“”中的“ host1,2,3,4:” session2”应该保持不变。 If it is not understandable, I can explain more, I have written this flaw code which is not writing back to the file, is there any way to do it better? 如果无法理解,我可以解释更多,我已经编写了此缺陷代码,但没有写回到文件中,有什么办法可以做得更好?

#!/usr/bin/python

File1 = open ("test","rw+")

Read1 = File1.read()

Spl = Read1.split()

Inp_Host = raw_input("Enter the host input:")

Side_screen = str(raw_input ("up, down, left, right, choose your way:"))

#if Inp_Host == Spl[2] or Spl [3] or Spl [4] or Spl [5]:
#   print "The host already exists"

#else:

if Side_screen == "up":
    Spl[2] = Spl[9] = Inp_Host
        Spl[12] = input ()

elif Side_screen == "down":
         Spl[3] = Spl[13] = Inp_Host
         Spl[16] = input ()

elif Side_screen == "left":
         Spl[4] = Spl[17] = Inp_Host
         Spl[20] = input ()

elif Side_screen == "right":
         Spl[5] = Spl[21] = Inp_Host
         Spl[24] = input ()

Spl.write()

print Spl.read()

And I'm not sure whether editing a file by splitting will put back the file in same indention or not. 而且我不确定通过拆分编辑文件是否会将文件放回相同的缩进中。

There are a number of problems with your code. 您的代码有很多问题。 First of all, if you want to write to the file "test" in order to modify its contents, you will have to call file.write() , not Spl.write() . 首先,如果要写入文件“ test”以修改其内容,则必须调用file.write()而不是Spl.write() Also, I believe the file mode "rw+" is invalid; 另外,我认为文件模式"rw+"无效; to open a file for both reading and writing, you would want just "r+" ; 要打开一个用于读写的文件,您只需要"r+" please see here for details. 请参阅此处了解详细信息。 Additionally, when you open a file for both reading and writing, the writing is usually appended to the end of the file unless you specify otherwise. 此外,当您同时打开文件进行读取和写入时,除非您另外指定,否则写入通常会附加到文件的末尾 If you want to overwrite starting at a random point in the file, or from the beginning, then you will need to use file.seek() , and you may wish to look into file.truncate() as well. 如果要从文件中的随机点开始或从头开始覆盖,则将需要使用file.seek() ,并且您可能还希望研究file.truncate() You can learn more about file object methods here . 您可以在此处了解有关文件对象方法的更多信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM