在Python中更改列表的值

时间:2019-01-09 15:37:01

标签: python python-3.x list iteration slice

我有一个哈希列表,例如:

@IBAction func login(_sender: Any) {

}

我需要将所有第二个“列”(./.bash_history)修改为唯一的名称。如下所示:

f71bc81a7e7d649f0d279b5861972eae  ./.bash_history
22bfb8c1dd94b5f3813a2b25da67463f  ./.bash_logout
1f98b8f3f3c8f8927eca945d59dcc1c6  ./.bashrc
f4e81ade7d6f9fb342541152d08e7a97  ./.profile
595dbcceb1b5921f7fad73ad17ec1fe4  ./.python_history
a23829cea89e42d79df01428e550191a  ./.viminfo

我无法简单地提出一种方法。 (请记住,哈希和名称之间的空格)。我尝试切片,遍历它并更改值,但没有得到想要的结果。

我尝试过的事情:

    f71bc81a7e7d649f0d279b5861972eae  App1
    22bfb8c1dd94b5f3813a2b25da67463f  App1
    1f98b8f3f3c8f8927eca945d59dcc1c6  App1
    f4e81ade7d6f9fb342541152d08e7a97  App1

任何帮助将不胜感激。 谢谢。

3 个答案:

答案 0 :(得分:0)

我不确定100%的目标是什么,但是基于我想您想要的,我相信这段代码就足够了。

replaced_value = 'App1'

with open('md5sum.txt') as fin:
  with open('md5sum_modified.txt', 'w') as fout:
    for line in fin:
      values = line.split()
      values[1] = replaced_value
      new_line = ' '.join(values)
      fout.write(new_line + '\n')

答案 1 :(得分:0)

创建文件

t = """f71bc81a7e7d649f0d279b5861972eae  ./.bash_history
22bfb8c1dd94b5f3813a2b25da67463f  ./.bash_logout
1f98b8f3f3c8f8927eca945d59dcc1c6  ./.bashrc
f4e81ade7d6f9fb342541152d08e7a97  ./.profile
595dbcceb1b5921f7fad73ad17ec1fe4  ./.python_history
a23829cea89e42d79df01428e550191a  ./.viminfo"""

fn = "md5sum.txt"
with open(fn,"w")as f:f.write(t)

逐行读取和处理文件

data = []
with open(fn) as f, open("changed.txt","w") as w:
    for line in f:
        if line.strip():
            # split at spaces
            h, _ = line.strip().split()
            # ignore _ - simply give it a new one
            w.write(f"{h} all_the_same_name\n")

with open("changed.txt") as f: 
    print(f.read())

输出:

f71bc81a7e7d649f0d279b5861972eae all_the_same_name
22bfb8c1dd94b5f3813a2b25da67463f all_the_same_name
1f98b8f3f3c8f8927eca945d59dcc1c6 all_the_same_name
f4e81ade7d6f9fb342541152d08e7a97 all_the_same_name
595dbcceb1b5921f7fad73ad17ec1fe4 all_the_same_name
a23829cea89e42d79df01428e550191a all_the_same_name

答案 2 :(得分:0)

您可以使用<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="rule 1Q" stopProcessing="true"> <match url="!(/$|\.)" /> <action type="Rewrite" url="%{REQUEST_URI}/" /> </rule> <rule name="rule 2a" stopProcessing="true"> <match url="^(en)/rooms/([a-zA-Z0-9-_]+)/?$" /> <action type="Rewrite" url="/index.php?lang={R:1}&page=room&room_id={R:2}" appendQueryString="true" /> </rule> <rule name="rule 3a" stopProcessing="true"> <match url="^(en)/([a-zA-Z0-9-_]+)/?$" /> <action type="Rewrite" url="/index.php?lang={R:1}&page={R:2}" appendQueryString="true" /> </rule> <rule name="rule 4a" stopProcessing="true"> <match url="^(en)/?$" /> <action type="Rewrite" url="/index.php?lang={R:1}" appendQueryString="true" /> </rule> <rule name="rule 5a" stopProcessing="true"> <match url="^rooms/([a-zA-Z0-9-_]+)/?$" /> <action type="Rewrite" url="/index.php?page=room&room_id={R:1}" appendQueryString="true" /> </rule> <rule name="rule 6a" stopProcessing="true"> <match url="^([a-zA-Z0-9-_]+)/?$" /> <action type="Rewrite" url="/index.php?page={R:1}" appendQueryString="true" /> </rule> </rules> </rewrite> <httpErrors> ... </httpErrors> </system.webServer> </configuration> 进行操作,如下所示:

str.replace()

这将是f = open(r"md5sum.txt","r") lines = f.readlines() new_read = [] for line in lines: line = line.replace(line.split(' ')[2], 'Appl') new_read.append(line) 列表的输出:

new_read
相关问题