我是Python的新手.我希望能够打开一个文件,并通过Python替换给定替换的某些单词的每个实例.例如,用'bo'替换每个单词'zero',用'bob'替换'temp',用'nothing'说'trash'.
我第一次开始使用它:
for line in fileinput.input(fin):
fout.write(line.replace('zero', '0'))
fout.write(line.replace('temp','bob'))
fout.write(line.replace('garbage','nothing'))
Run Code Online (Sandbox Code Playgroud)
但我不认为这是一种甚至是远程正确的方法.然后我考虑使用if语句来检查行是否包含这些项,如果它包含这些项,然后替换该行包含的那个,但是根据我所知的Python,这也不是真正理想的解决方案.我很想知道最好的方法是什么.提前谢谢!
我试图从多个输入文件中读取并将每个文件中的第二行作为表格彼此相邻打印
import sys
import fileinput
with fileinput.input(files=('cutflow_TTJets_1l.txt ', 'cutflow_TTJets_1l.txt ')) as f:
for line in f:
proc(line)
def proc(line):
parts = line.split("&") # split line into parts
if "&" in line: # if at least 2 parts/columns
print parts[1] # print column 2
Run Code Online (Sandbox Code Playgroud)
但我得到一个"AttributeError:FileInput实例没有属性' __exit__'"
我想在文件中特定行的特定列插入一个字符串.
假设我有一个文件 file.txt
How was the English test?
How was the Math test?
How was the Chemistry test?
How was the test?
Run Code Online (Sandbox Code Playgroud)
我想How was the History test?通过History在第4行第13列添加字符串来更改最后一行.
目前我读取文件的每一行并将字符串添加到指定的位置.
with open("file.txt", "r+") as f:
# Read entire file
lines = f.readlines()
# Update line
lino = 4 - 1
colno = 13 -1
lines[lino] = lines[lino][:colno] + "History " + lines[lino][colno:]
# Rewrite file
f.seek(0)
for line in lines:
f.write(line)
f.truncate()
f.close()
Run Code Online (Sandbox Code Playgroud)
但我觉得我应该能够简单地将行添加到文件中,而无需读取和重写整个文件.
我需要在IP地址发生变化时更新文本文件,然后再从shell运行一些命令.
创建变量LASTKNOWN ="212.171.135.53"这是我们编写此脚本时的IP地址.
获取当前的IP地址.它会每天改变.
为新IP创建变量CURRENT.
比较(作为字符串)CURRENT到LASTKNOWN
如果它们相同,则退出()
如果他们不同,
A.将包含LASTKNOWN IP地址的旧配置文件(/etc/ipf.conf)"复制"到/ tmp B.用/tmp/ipf.conf文件中的CURRENT替换LASTKNOWN.
C.使用子进程"mv /tmp/ipf.conf /etc/ipf.conf"D
.使用子进程执行,"ipf -Fa -f /etc/ipf.conf"E
.使用子进程执行,"ipnat -CF -f /etc/ipnat.conf"
出口()
我知道如何执行第1步到第6步.我堕落在"文件编辑"部分,A - > C.我无法分辨使用什么模块或是否应该编辑文件.有很多方法可以做到这一点,我无法决定最好的方法.我想我想要最保守的一个.
我知道如何使用子进程,所以你不需要对它进行评论.
我不想替换整条线; 只是一个特定的点缀四边形
谢谢!
首先感谢帮助我移动文件和帮助我使用tcl脚本.
小疑问我有python代码..如下所示..
import os
import shutil
data =" set filelid [open \"C:/Sanity_Automation/Work_Project/Output/smokeTestResult\" w+] \n\
puts $filelid \n\
close $filelid \n"
path = "C:\\Sanity_Automation\\RouterTester900SystemTest"
if os.path.exists(path):
shutil.rmtree('C:\\Sanity_Automation\\RouterTester900SystemTest\\')
path = "C:\\Program Files (x86)"
if os.path.exists(path):
src= "C:\\Program Files (x86)\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest"
else:
src= "C:\\Program Files\\abc\\xyz\\QuickTest\\Scripts\\RouterTester900\\Diagnostic\\RouterTester900SystemTest"
dest = "C:\\Sanity_Automation\\RouterTester900SystemTest\\"
shutil.copytree(src, dest)
log = open('C:\\Sanity_Automation\\RouterTester900SystemTest\\RouterTester900SystemTest.app.tcl','r+')
log_read=log.readlines()
x="CloseAllOutputFile"
with open('C:\\Sanity_Automation\\RouterTester900SystemTest\\RouterTester900SystemTest.app.tcl', 'a+') as fout:
for line in log_read:
if x in line:
fout.seek(0,1)
fout.write("\n")
fout.write(data)
Run Code Online (Sandbox Code Playgroud)
此代码用于将文件从一个位置复制到另一个位置,在特定文件中搜索关键字并将数据写入文件正在运行...
我的疑问是每当我写的..它写到文件末尾而不是当前位置...
示例:说..我将文件从程序文件复制到sanity文件夹,并在其中一个复制文件中搜索单词"CloseAllOutputFile".当找到单词时,它应该在该位置插入文本而不是文件末尾.
我从这里获取示例代码。
f1 = open('file1.txt', 'r')
f2 = open('file2.txt', 'w')
for line in f1:
f2.write(line.replace('old_text', 'new_text'))
f1.close()
f2.close()
Run Code Online (Sandbox Code Playgroud)
但是我不知道如何用各自的新词替换多个词。在这个例子中,如果我想找到一些喜欢的单词,(old_text1,old_text2,old_text3,old_text4)并用它们各自的新单词替换(new_text1,new_text2,new_text3,new_text4)。
提前致谢!
我有一个文本文件 (*.txt),它的内容是"01110011",我想替换它:'00' ==> a, '01' ==> b, '10' ==> c,'11' ==> d 从左到右。所以内容变成'bdad'. 根据这篇文章,我使用了下面的代码,但不幸的是,替换不是定向的(我的意思是不是从左到右)。我可以请你帮助我吗?
# Read in the file
with open('file.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('00', 'a')
filedata = filedata.replace('01', 'b')
filedata = filedata.replace('10', 'c')
filedata = filedata.replace('11', 'd')
# Write the file out again
with open('file.txt', 'w') as file:
file.write(filedata)
Run Code Online (Sandbox Code Playgroud) 对于数学游戏,我有一个包含等级,等式和答案(由制表符分隔)的文件.例如:
Lvl Eq Ans
2 2*6 12
Run Code Online (Sandbox Code Playgroud)
如何,在新文件中用逗号替换制表符空间?