[英]Python File String Replace Dict and Tuple
have a Dict with multiple values in a tuple.在元组中有一个带有多个值的字典。
newhost = {'newhost.com': ('1.oldhost.com',
'2.oldhost.com',
'3.oldhost.com',
'4.oldhost.com')
}
I wanna open a existing file and search for lines in this file that contains a value of the oldhosts.我想打开一个现有文件并在该文件中搜索包含 oldhosts 值的行。 A file can have multiple Account Lines.
一个文件可以有多个帐户行。 In example Account: 1.oldhost.com username Account: someotherhost username When the line with 1.oldhost.com or 2.oldhost.com or 3.oldhost.com and so on is found i wanna replace it with the key form the dict newhost.com.
在示例帐户中:1.oldhost.com 用户名帐户:someotherhost 用户名当找到带有 1.oldhost.com 或 2.oldhost.com 或 3.oldhost.com 等的行时,我想用字典中的键替换它新主机.com。
Can anyone help me?谁能帮我? Searched alot, but didnt find the right thing.
搜索了很多,但没有找到正确的东西。
Regards问候
Maybe something like this could get you started也许这样的事情可以让你开始
infile_name = 'some_file.txt'
# Open and read the incoming file
with open(infile_name, 'r') as infile:
text = infile.read()
# Cycle through the dictionary
for newhost, oldhost_list in host_dict.items():
# Cycle through each possible old host
for oldhost in oldhost_list:
text.replace(oldhost, newhost)
outfile_name = 'some_other_file.txt'
# Write to file
with open(outfile_name, 'w') as outfile:
outfile.write(text)
Not claiming this to be the best solution, but it should be a good start for you.并不是说这是最好的解决方案,但这对您来说应该是一个好的开始。
To easily find the new host for a given old host, you should convert your data structure:要轻松找到给定旧主机的新主机,您应该转换数据结构:
# your current structure
new_hosts = {
'newhost-E.com': (
'1.oldhost-E.com',
'2.oldhost-E.com',
),
'newhost-A.com': (
'1.oldhost-A.com',
'2.oldhost-A.com',
'3.oldhost-A.com',
),
}
# my proposal
new_hosts_2 = {
v: k
for k, v_list in new_hosts.items()
for v in v_list}
print(new_hosts_2)
# {
# '1.oldhost-E.com': 'newhost-E.com',
# '2.oldhost-E.com': 'newhost-E.com',
# '1.oldhost-A.com': 'newhost-A.com',
# '2.oldhost-A.com': 'newhost-A.com',
# '3.oldhost-A.com': 'newhost-A.com',
# }
This does repeat the new host names (the values in new_hosts_2
), but it will allow you to quickly look up given an old host name:这确实会重复新主机名(
new_hosts_2
的值),但它可以让您快速查找旧主机名:
some_old_host = 'x.oldhost.com'
the corresponding_new_host = new_hosts_2[some_old_host]
Now you just need to:现在你只需要:
new_hosts_2
new_hosts_2
查找对应的新主机Maybe like this:也许是这样的:
with open(file_name_1, 'r') as fr:
with open(file_name_2, 'w') as fw:
for line in fr:
line = line.strip()
if len(line) > 0:
# logic to find the start and end position of the old host
start_i = ?
end_i = ?
# get and replace, but only if its found in 'new_hosts_2'
old_host = line[start_i:end_i]
if old_host in new_hosts_2:
line = line[:start_i] + new_hosts_2[old_host] + line[end_i:]
fw.write(line + '\n')
Thank you for your tips.谢谢你的提示。 I came up with this now and it is working fine.
我现在想出了这个,它工作正常。
import fileinput
textfile = 'somefile.txt'
curhost = 'newhost.com'
hostlist = {curhost: ('1.oldhost.com',
'2.oldhost.com',
'3.oldhost.com')
}
new_hosts_2 = {
v: k
for k, v_list in hostlist.items()
for v in v_list}
for line in fileinput.input(textfile, inplace=True):
line = line.rstrip()
if not line:
continue
for f_key, f_value in new_hosts_2.items():
if f_key in line:
line = line.replace(f_key, f_value)
print line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.