0

(rephrasing the question for better clarity.)

Suppose I have 1000 text files. Each file is a textual form, with fixed field descriptions and headers, but the data is different. For example, file 1:

The Foo Factory             Date:  2015-10-02

Order Details
-------------------------
Order ID:          [   1    ]  Amount: [   1 ]
Order Price:       [  12.34 ]

Have a nice day.

and file 2:

The Foo Factory             Date:  2016-01-11

Order Details
-------------------------
Order ID:          [   7    ]  Amount: [  17 ]
Order Price:       [ 543.21 ]

Have a nice day.

etc. Now, suppose I went ahead and translated the field descriptions into French; and I deleted the values. So now I have:

L'usine Foo                 Date:            

Détails de commande
-------------------------
Nm.du Commande:   [        ]  Montant: [     ]
Prix du Commande: [        ]

Bonne journée.

I don't really speak French so ignore any mistakes here. Anyway, the positions of all fields are exactly the same, but the non-field-value text is different. I want to apply this replacement to all of the files. How do I achieve that?

15
  • 1
    What's your end goal?
    – user193661
    Sep 30, 2015 at 16:08
  • @Clearquestionwithexamples: Changing the field descriptions for a textual "form output"; and I will eventually have many of these. Also, your nick is rather confusing. I don't mean to be rude, but - perhaps you should consider changing it.
    – einpoklum
    Sep 30, 2015 at 16:10
  • Can you provide a before and after screenshot or use a code block to provide an example?
    – user193661
    Sep 30, 2015 at 16:16
  • you "replaced the "template" text and dropped the "file-specific" data" that sounds like the file would now be empty.
    – user193661
    Sep 30, 2015 at 16:18
  • Are you aware of regex?
    – user193661
    Sep 30, 2015 at 16:21

1 Answer 1

0

You've already generated the formatting map so the only remaining work is regex replace. Generating the map is more difficult, especially for larger, more complicated text. I use Sublime Text for that. But the use case and further information isn't clear here, you should take it to softwarerecs.SE if it's important. I'm including a Python version anyway:

 

# -*- coding: utf-8 -*-
import os

SOURCE_DIRECTORY = ''
DEST_DIRECTORY = ''

replacements = ["L'usine Foo                 ",
"Détails de commande",
"Nm.du Commande:   ",
"Montant: ",
"Prix du Commande: ",
"Bonne journée."]

samples = ["The Foo Factory             ",
"Order Details",
"Order ID:          ",
"Amount: ",
"Order Price:       ",
"Have a nice day."]

for filename in os.listdir(SOURCE_DIRECTORY):
    with open(os.path.join(SOURCE_DIRECTORY, filename), 'r') as nctn:
        text = nctn.read()
    for i, val in enumerate(samples):
        text = text.replace(val, replacements[i])
    with open(os.path.join(DEST_DIRECTORY, filename), 'w') as nctn:
        nctn.write(text)
2
  • What I mean is that, if you have a very large file, the very act of determining what operations need to be done, would be laborious. Think, manually selecting a thousand lines of keywords to create your mapping with.
    – user193661
    Oct 2, 2015 at 23:01
  • Oh, no no no, I don't want want to write a custom script that contains the pattern. I need something which takes any (new pattern file,working file) pair and does the job.
    – einpoklum
    Oct 2, 2015 at 23:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .