在 Python 3.xx 中创建多个文件

Creating multiple files in Python 3.xx

我想创建单个 .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'] - id much更喜欢它来展示 Cooper Raymond。 (我找到了这个 csv 部分的修复 :)) 对于其他正在寻找

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

谢谢。

如何创建一个函数来调用第一部分以获取台词,并将所有内容放入 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()