How do i replace a single number (from a set of lines) from a list of numbers?

后端 未结 1 613
耶瑟儿~
耶瑟儿~ 2021-01-29 10:38

i am new to programming/macroing,etc and i would like to do this. Should i use word 2007 or Python or something else? i have only those two things currently.

or         


        
相关标签:
1条回答
  • 2021-01-29 11:13

    A simple Python script will work:

    template = """hello how are you ref={}
    ofdifaoididpodvjakjeoijknvkln
    ikvnoivwoeij,vn,nviojv
    nkavjoiewgkjvlkaior
    """
    
    replace = [
        876191, 11166524, 1117225, 1116953, 798993,
        793519, 1116737, 1116691, 1116364, 1115635,
        909014, 760195, 689981, 604787, 1116217,
        1104482, 869990, 886290, 1115893]
    
    print ''.join([template.format(r) for r in replace])
    

    Which will display:

    hello how are you ref=876191
    ofdifaoididpodvjakjeoijknvkln
    ikvnoivwoeij,vn,nviojv
    nkavjoiewgkjvlkaior
    hello how are you ref=11166524
    ofdifaoididpodvjakjeoijknvkln
    ikvnoivwoeij,vn,nviojv
    nkavjoiewgkjvlkaior
    hello how are you ref=1117225
    ofdifaoididpodvjakjeoijknvkln
    ikvnoivwoeij,vn,nviojv
    nkavjoiewgkjvlkaior
    .
    .
    .
    

    and so on.

    If you would like to write the output directly to a text file, the following will work:

    with open('output.txt', 'w') as f_output:
        f_output.write(''.join([template.format(r) for r in replace]))
    

    If you would also like to read the numbers from a text file, use the following:

    with open('numbers.txt', 'r') as f_input, open('output.txt', 'w') as f_output:
        for number in f_input:
            f_output.write(template.format(number.strip()))
    
    0 讨论(0)
提交回复
热议问题