简体   繁体   中英

Python - Take parts of a txt file and put them in another txt

I have a large file with the following syntax:

Object 1:
[Useless Data]
com_pos = number number number
[Useless Data]

Object 2:
[Useless Data]
com_pos = number, number, number
[Useless Data]
...
and so on (there's a very large number of objects.).

What I want to do is pick the numbers and put them in another txt file with a specific format (basically a row for each object and a column for each number).

The problem is I have the same com_pos = for every object. How should I do that? Should I use Regular Expressions?

You have to write some kind of parser for this. You don't need to use regular expressions if you don't understand them. For example, given your two examples, this would work just as well:

with open(path) as f:
    for line in f:
        columns = line.split()
        if columns[0] == 'com_pos' and columns[1] == '=':
            numbers = [float(column.rstrip(',')) for column in columns[2:]]
            # do something with numbers

Using regular expressions can make things more compact, more efficient, or more robust. For example, consider this:

r = re.compile(r'com_pos\s*=\s*(\d+),?\s*(\d+),?\s*(\d+)')
with open(path) as f:
    for line in f:
        m = r.search(line)
        if m:
            numbers = [float(group) for group in m.groups]
            # do something with numbers

That will probably run faster, and it's more robust in the face of variable input (a data format that sometimes has commas and sometimes doesn't looks a lot like a human-written file…), and it's simpler if you understand the regexp. But if you don't, it'll be harder to maintain.


com_pos\s*=\s*(\d+),?\s*(\d+),?\s*(\d+)

正则表达式可视化

Debuggex Demo

You can use the following :

with open ('first_file' ,'r') as f1 and open('second_file' ,'w') as f2 :
 for line in f1.readlines() :
    if 'com_pos' in line :
       f2.write(line.split('=')[1])

first you need to find the line that com_pos is in it , then you can split that line with = and write the second splited element that is the numbers in second file .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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