简体   繁体   English

Python循环从文件的每一行获取数据并将其转换为dec

[英]Python loop to get data from every line of file and convert it to dec

I'm kinda lost at the moment: I have text file with lines looking like that:我现在有点迷茫:我有文本文件,行看起来像这样:

/dev/input/event0: 0003 0035 000002ac 
/dev/input/event0: 0003 0036 000008fb

There are many lines like that, and I want to make script that gets the last hex value from every line and then writes them in file with output looking like this:有很多这样的行,我想制作脚本,从每一行获取最后一个十六进制值,然后将它们写入文件,输出如下所示:

something someting hex_from_line_1 hex_from_line_2 
something someting hex_from_line_3 hex_from_line_4 

And so on.等等。 As I'm new to Python I have some trouble with making loop which would do this.因为我是 Python 的新手,所以我在制作循环时遇到了一些麻烦。 Can You give me any guidance?你能给我任何指导吗? (I'm not asking for whole loop, only guidance - I'd love to learn that, not use some finished code) (我不是要求整个循环,只是指导 - 我很想学习,而不是使用一些完成的代码)

A fun thing about Python's zip is that it will happily take the same iterator multiple times as arguments, allowing you to pair up inputs easily. Python zip一个有趣之处在于,它很乐意多次将相同的迭代器用作参数,从而使您可以轻松地配对输入。 For example:例如:

# For efficiency, if you're on Python 2, include this line so zip is a generator that produces pairs on demand, rather than eagerly slurping the whole file
from future_builtins import zip

with open('myinput') as f:
    # Creates a generator that produces only the final space separated value for each line (could be anything; not checking for hex)
    final_hex = (line.rsplit(None, 1)[-1] for line in f)
    # By using the same generator twice, we get the 1st, 3rd, 5th, etc. from one
    # and the 2nd, 4th, 6th, etc. from the other.
    for hexa, hexb in zip(final_hex, final_hex):
        print("something something", hexa, hexb) # Python 3 print function
        print "something something", hexa, hexb  # Python 2 print statement

Note: If the input data isn't an even number of lines, this will drop the final unpaired input.注意:如果输入数据不是偶数行,这将丢弃最终未配对的输入。 You can use itertools.zip_longest ( izip_longest on Python 2) if you want the unpaired value.如果需要未配对的值,可以使用itertools.zip_longest (Python 2 上的izip_longest )。

I'm kinda lost at the moment: I have text file with lines looking like that: /dev/input/event0: 0003 0035 000002ac /dev/input/event0: 0003 0036 000008fb There are many lines like that, and I want to make script that gets the last hex vaule from every line and then writes them in file with output looking like this: something someting hex_from_line_1 hex_from_line_2 something someting hex_from_line_3 hex_from_line_4 And so on.我现在有点迷茫:我有这样的文本文件: /dev/input/event0: 0003 0035 000002ac /dev/input/event0: 0003 0036 000008fb 有很多这样的行,我想make 脚本从每一行获取最后一个十六进制值,然后将它们写入文件中,输出如下所示:something hex_from_line_1 hex_from_line_2 something someting hex_from_line_3 hex_from_line_4 等等。 As I'm new to Python I have some trouble with making loop which would do this.因为我是 Python 的新手,所以我在制作循环时遇到了一些麻烦。 Can You give me any guidance?你能给我任何指导吗? (I'm not asking for whole loop, only guidance - I'd love to learn that, not use some finished code) (我不是要求整个循环,只是指导 - 我很想学习,而不是使用一些完成的代码)

Loop over the lines in the file like this像这样循环文件中的行

with open('filename.txt','r') as f:
    for line in f:
        hexNibble=line[-1] #this is a string
        hexValue=int(hexNibble,16) #this is an integer
        #Now you have the integer value of the last hex nibble on the line
        #So...
        #Continue doing whatever you want to do with it...

file.txt文件.txt

/dev/input/event0: 0003 0035 000002ac 
/dev/input/event0: 0003 0036 000008fb
/dev/input/event0: 0003 0035 000002ad 
/dev/input/event0: 0003 0036 000008fe
/dev/input/event0: 0003 0035 000002af 
/dev/input/event0: 0003 0036 000008fg
/dev/input/event0: 0003 0036 000008fz

code代码

with open('file.txt', 'rb') as f:
    data = [x.split(': ')[1].strip() for x in f.read().splitlines()]

with open('output.txt', 'wb') as f:
    for i in range(0, len(data), 2):
        f.write('something something {0}\n'.format(' '.join(data[i:i+2])))

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

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