我想创建单个.xml文件,每个文件中都有一个标准模板,每次只更改几个关键位数据。 我可以这样做,虽然有点乱。
我想要自动化的结构是一个文件夹(已经设置),我运行这个脚本,它生成用户定义的文件数量1,10,50,100等等,但每个文件都是file_001.xml file_002。 xml file_003.xml等。 现在我已经达到了可以做到这一点但我每次想要一个新文件时都必须运行脚本。
我觉得我错过了一些明显的东西。
以下是代码现在的样子。
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)
如果有人有解决方案,请听我的意见。
此外,这里有一些我已经尝试过的解决方案
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)
和
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()
最后一件事,作为一个额外的问题,任何人都可以阐明我如何从我的.csv文件打印数据,它显示如['Cooper'] ['Raymond'] - 我更喜欢它显示Cooper Raymond 。 (我找到了这个csv部分的修复:)) 对于任何其他人看
with open('names_m.csv', 'r')as f:
for line in f:
line.strip()
male = list(f)
感谢。
答案 0 :(得分:0)
如何创建一个函数来调用第一部分来获取行,并将所有内容放在while循环中?
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()