Search code examples
pythonpython-3.xautomationfile-manipulationuat

Creating multiple files in Python 3.xx


I am wanting to create single .xml files with a standard template in each, only a few key bits of data change each time. This i can do, albeit a little messy.

The structure i want to automate is have a folder (already set up) where i run this script and it generates a user defined number of files 1, 10, 50, 100 etc etc but each file is file_001.xml file_002.xml file_003.xml and so on. Now i have gotten to a point where i can do it but i have to run the script each time i want a new file.

I feel i am missing something glaringly obvious.

Here's what the code looks like right now.

import random
import time
import glob
import os
import csv

# Importing random names & Sample types
with open('names_m.csv', 'r')as f:
    reader = csv.reader(f)
    male = list(reader)
with open('names_f.csv', 'r')as f:
    reader = csv.reader(f)
    female = list(reader)
with open('surnames.csv', 'r')as f:
    reader = csv.reader(f)
    surname = list(reader)
with open('sampletype.csv', 'r')as f:
    reader = csv.reader(f)
    stype = list(reader)

# getting today's date to put into line 4,5,6
date = time.strftime("%Y%m%d")
# string_1 Unique reference number
string_1 = random.randrange(1000000000, 9999999999)
# string_2 patient ID number
string_2 = 'P999990'
# string_3 ward selection
string_3 = random.choice(['W1', 'W2', 'F1', 'F2'])
# string_4 date + order number (string_5)
string_4 = date
# string_5 sample order number.
string_5 = random.randrange(00000000, 99999999)
string_6 = random.choice(surname)
string_7 = random.choice(male)
string_9 = random.choice(['M', 'F'])
# string_8 sample type.
string_8 = random.choice(stype)

# HL7 Message.
line1 = "MSH|^~\&|RHM||||||201702141105||ORM^O01|%s|P|2.5||NE|AL|||| \n" % (string_1)
line2 = "PID|1||%s^^^^HOSPITALNO~^^^^NHSNO||%s^%s||190701190000|%s|||||||||||||| \n" % (string_2, string_6, string_7, string_9)
line3 = "PV1|1||%s|||||||||||||||||||||||||||||||||||||||||||||||| \n" % (string_3)
line4 = "ORC|NW|%s%s||%s|||1^^^201702144500^^R||^^^20170214104500^^^^|||Test001||||REASON||||\n" % (string_4, string_5, string_5)
line5 = "OBR|1|%s%s||%s|||2017021411045|201702141045||Test001||||||||||\n" % (string_4, string_5, string_8)
line6 = "OBX|1|ST|%s%s||20170214%s|||||||||||||||\n" % (string_4, string_5, string_5)
line7 = "SPM|1|||||||||||||||||||||||||||||\n"
"""
# How many new files we want creating.
filecopy = input("How many files are to be created?:")
files = filecopy
"""
filecopy = 100 

i = 1
while os.path.exists("S360_%s.xml" % i):
    i += 1
if i == filecopy:
    f.close()
else:
    f = open('S360_%s.xml' % i, "w")
f.write(line1 + line2 + line3 + line4 + line5 + line6 + line7)

If anyone has a solution please, i am all ears.

Also, here are some solutions i have already tried

files = [1] = +1

for files in files:
    with open('S360_{}.xml'.format(files), "w") as f:
        f.write(line1 + line2 + line3 + line4 + line5 + line6 + line7)

and

os.chdir("C:\\UAT DATA")
for file in glob.glob("*.xml"):
    f = open((file.rsplit(".", 1)[0])+"xml", "w")
    f.write(line1 + line2 + line3 + line4 + line5 + line6 + line7)
    f.close()

And one final thing, as a bonus question, could anyone shed light on how when i print data from my .csv files it shows like ['Cooper'] ['Raymond'] - id much prefer it to display Cooper Raymond. ( i found a fix to this csv part :) ) for anyone else looking

with open('names_m.csv', 'r')as f:
for line in f:
    line.strip()
    male = list(f)

Thanks.


Solution

  • How about creating a function to call the first part to get the lines, and put everything in the while loop?

    import random
    import time
    import glob
    import os
    import csv
    
    def get_lines():
        # Importing random names & Sample types
        with open('names_m.csv', 'r')as f:
            reader = csv.reader(f)
            male = list(reader)
        with open('names_f.csv', 'r')as f:
            reader = csv.reader(f)
            female = list(reader)
        with open('surnames.csv', 'r')as f:
            reader = csv.reader(f)
            surname = list(reader)
        with open('sampletype.csv', 'r')as f:
            reader = csv.reader(f)
            stype = list(reader)
    
        # getting today's date to put into line 4,5,6
        date = time.strftime("%Y%m%d")
        # string_1 Unique reference number
        string_1 = random.randrange(1000000000, 9999999999)
        # string_2 patient ID number
        string_2 = 'P999990'
        # string_3 ward selection
        string_3 = random.choice(['W1', 'W2', 'F1', 'F2'])
        # string_4 date + order number (string_5)
        string_4 = date
        # string_5 sample order number.
        string_5 = random.randrange(00000000, 99999999)
        string_6 = random.choice(surname)
        string_7 = random.choice(male)
        string_9 = random.choice(['M', 'F'])
        # string_8 sample type.
        string_8 = random.choice(stype)
    
        # HL7 Message.
        line1 = "MSH|^~\&|RHM||||||201702141105||ORM^O01|%s|P|2.5||NE|AL|||| \n" % (string_1)
        line2 = "PID|1||%s^^^^HOSPITALNO~^^^^NHSNO||%s^%s||190701190000|%s|||||||||||||| \n" % (string_2, string_6, string_7, string_9)
        line3 = "PV1|1||%s|||||||||||||||||||||||||||||||||||||||||||||||| \n" % (string_3)
        line4 = "ORC|NW|%s%s||%s|||1^^^201702144500^^R||^^^20170214104500^^^^|||Test001||||REASON||||\n" % (string_4, string_5, string_5)
        line5 = "OBR|1|%s%s||%s|||2017021411045|201702141045||Test001||||||||||\n" % (string_4, string_5, string_8)
        line6 = "OBX|1|ST|%s%s||20170214%s|||||||||||||||\n" % (string_4, string_5, string_5)
        line7 = "SPM|1|||||||||||||||||||||||||||||\n"
    
        return line1 + line2 + line3 + line4 + line5 + line6 + line7
    
    i = 1
    files = int(input("how many:"))
    
    while os.path.exists("S360_%s.xml" % i):
        i += 1
        if i == files:
            print("complete")
        else:
            f = open('S360_%s.xml' % i, "w+")
            lines_to_write = get_lines()
            f.write(lines_to_write)
            f.close()