0

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.

Lloyd
  • 75
  • 5
  • Encapsulate your code for creating a single file in a function. That function should accept as input everything that isn't constant in the files, all the things that could be different from one file to another. Then, you just write a loop that, for each iteration, calls the function that creates files. – Patrick Haugh Mar 13 '17 at 14:39
  • @PatrickHaugh thank you for your response, please forgive by ignorance, but i have no idea what you mean. Are you saying (in my first block of code Line 52 & 57 i should try f = open("S360_"(%s)".xml" %i,"w") ? – Lloyd Mar 13 '17 at 14:48

1 Answers1

0

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()
Glrs
  • 1,060
  • 15
  • 26
  • This is a great idea! i got it to work, by mixing things up a little. i'm sorry i wasnt clear, but i wanted, if there was no file already, for it to start at 1. So i added this code.`i = 1 f = open('S360_%s.xml' % i, "w+") lines_to_write = get_lines() f.write(lines_to_write) i = 1 x = int(input("how many:")) files = x 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)` – Lloyd Mar 13 '17 at 22:56
  • Nice to hear you solved it! I have updated my answer with part of your code, so now it should work properly. Don't forget to close the file after you're done, with `f.close()`. Please accept my answer if it answers your question. – Glrs Mar 14 '17 at 07:53
  • Thank you for your help! :) – Lloyd Mar 14 '17 at 08:54