简体   繁体   English

如何在python中搜索和替换文本文件的多个值?

[英]how to search and replace for multiple value of text file in python?

My data: 我的资料:

1 255 59 0 1 255 0 1 1 0 4 0 5 

0 1 255 1 253 0 90 1 1 0 2 0 233

I'm new to python and I need to replace all value in text file by use conditions: 我是python的新手,我需要通过使用条件替换文本文件中的所有值:

  1. If value in line = 255, Replace as 1 如果行中的值= 255,则替换为1

  2. If value in line < 255 (254, 253, 252...), Replace as 0 如果行中的值<255(254、253、252 ...),则替换为0

So, My target data is: 因此,我的目标数据是:

1 1 0 0 1 1 0 1 1 0 0 0 0 

0 1 1 1 0 0 0 1 1 0 0 0 0

How can I solve this problem? 我怎么解决这个问题?

Actually, I tried to fix it but doesn't work with my data. 实际上,我试图修复它,但不适用于我的数据。

This is my code: 这是我的代码:

f1 = open('20130103.txt','r')
f2 = open('20130103_2.txt','w')
count = 255
for line in f1:
    line = line.replace('255','1')
    count = count-1
    line = line.replace('%d'%(count), '0')
    f2.write(line)
f1.close()
f2.close()

And result is: 结果是:

1 1 59 0 1 1 0 1 1 0 4 0 5 
0 1 1 1 0 0 90 1 1 0 2 0 233

The eye catcher here is the number 255 , for which you would want to display 1 , which clearly indicates, you are only interested in the 8th bi t. 此处的引人注目的是数字255 ,您要为其显示1 ,该数字清楚地表明您只对第8th bi感兴趣。 The only case which looks strange is how your o/p not conforming with your requirement, does not manipulate 1 in which case, you have to leave it out of your transformation 唯一看起来很奇怪的情况是您的o / p如何不符合您的要求,在这种情况下无法操纵1 ,因此必须将其排除在转换之外

If I have to trust your requirement 如果我必须相信您的要求

>>> with open("test.in") as fin, open("test.out","w") as fout:
    for line in fin:
        line = (e >> 7 for e in map(int, line.split()))
        fout.write(''.join(map(str, line)))
        fout.write('\n')

If I have to trust your data 如果我必须信任您的数据

>>> with open("test.in") as fin, open("test.out","w") as fout:
    for line in fin:
        line = (e >> 7 if e != 1  else 1 for e in map(int, line.split()))
        fout.write(''.join(map(str, line)))
        fout.write('\n')

Another alternate view to this problem. 此问题的另一种观点。

>>> with open("test.in") as fin, open("test.out","w") as fout:
    for line in fin:
        line = (e / 255 if e != 1  else 1 for e in map(int, line.split()))
        fout.write(''.join(map(str, line)))
        fout.write('\n')

You could do something like this: 您可以执行以下操作:

with open('20130103.txt', 'r') as f1, open('20130103_2.txt', 'w') as f2:
    for line in f1:
        values = line.split()
        new_values = ' '.join('1' if value == '255' else '0' for value in values)

        f2.write(new_values + '\n')
  • line.split() splits the line into chunks. line.split()将行拆分为多个块。 'a b'.split() == ['a', 'b']
  • '1' if value == '255' else '0' for value in values is a generator that just outputs '1' or '0' , depending on the value of an item in your list of values. '1' if value == '255' else '0' for value in values是一个生成器,仅根据值列表中某项的值输出'1''0'
  • ' '.join() joins together the values in the list (or generator, in this case) with a space. ' '.join() )用空格将列表(或生成器' '.join()的值连接在一起。

Here's an example of how you can pass re.sub a function -- instead of a replace string -- to gain really fine control over how replacements are done: 这是一个示例,说明如何传递re.sub函数-而不是replace字符串-可以很好地控制替换的完成方式:

import re
lines = ['1 255 59 0 1 255 0 1 1 0 4 0 5',
     '0 1 255 1 253 0 90 1 1 0 2 0 233']
def do_replace(match):
    number = int(match.group(0))
    if number == 0 or number == 1:
        return str(number)
    elif number == 255:
        return '1'
    elif number < 255:
        return '0'
    else:
        raise ValueError
for line in lines:
    print re.sub(r'\d+', do_replace, line)

prints: 打印:

1 1 0 0 1 1 0 1 1 0 0 0 0
0 1 1 1 0 0 0 1 1 0 0 0 0

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

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