Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将输出随机化到文件_Python_Python 3.x_Uat - Fatal编程技术网

Python 将输出随机化到文件

Python 将输出随机化到文件,python,python-3.x,uat,Python,Python 3.x,Uat,我正在使用Python生成测试数据 我已经完成了整个过程,但是,我有这段代码 def get_lines(): line1 = "Document Header - Once per document" line2 = "\nDocument Information - Once per document" line3 = "\nDocument Information 2 - Once per document" line4 = "\nUser informa

我正在使用Python生成测试数据

我已经完成了整个过程,但是,我有这段代码

def get_lines():

    line1 = "Document Header - Once per document"
    line2 = "\nDocument Information - Once per document"
    line3 = "\nDocument Information 2 - Once per document"
    line4 = "\nUser information 1"
    line5 = "\nUser Information 1"
    line6 = "\nUser Information 1"
    line7 = "\nDocument Footer - Once per document"

    return line1 + line2 + line3 + line4 + line5 + line6 + line7
我希望能够用用户信息2,3,4填充第4,5,6行,如下所示:

line1 = "Document Header - Once per document"
line2 = "\nDocument Information - Once per document"
line3 = "\nDocument Information 2 - Once per document"   
line4 = "\nUser information 1"
line5 = "\nUser Information 1"
line6 = "\nUser Information 1"  
line4 = "\nUser information 2"
line5 = "\nUser Information 2"
line6 = "\nUser Information 2"
line4 = "\nUser information 3"
line5 = "\nUser Information 3"
line6 = "\nUser Information 3"
line7 = "\nDocument Footer - Once per document"
但是随机的,比如说我想要10个文件,一些包含一条用户信息,一些2到3个等等

我正在努力寻找一种始终如一的方式来生产我需要的东西

多谢各位

编辑:添加示例消息:ORC OBR和OBX都由UID链接

MSH|^~\&||||||||201705301105||ORM^O01|4960855009|P|2.5||NE|AL||||
PID|1||^^^^HOSPITALNO~^^^^NHSNO||Hendry^John||190203130000|F|||||||||||||| 
PV1|1||G2D|||||||||||||||||||||||||||||||||||||||||||||||| 
ORC|NW|2017053019783377||19783377|||1^^^201705304500^^R||^^^201705
OBR|1|2017053019783377||1019|||2017053011045|201705301045||Test001||||||||||
OBX|1|ST|2017053019783377||2017053019783377|||||||||||||||
SPM|1|||||||||||||||||||||||||||||
编辑

我现在看到您提供了示例数据,但我不确定这是您期望从
get_line
方法得到的所需输出,还是您将使用的输入从
get_line
产生所需输出


只需传递一个带有要打印的用户ID的变量。您还可以使用
random.choice
从列表中随机选择一个值,或者使用
random.randint

def get_lines(userid):

    line1 = "Document Header - Once per document"
    line2 = "\nDocument Information - Once per document"
    line3 = "\nDocument Information 2 - Once per document"
    line4 = "\nUser information {}".format(userid)
    line5 = "\nUser Information {}".format(userid)
    line6 = "\nUser Information {}".format(userid)
    line7 = "\nDocument Footer - Once per document"

    return line1 + line2 + line3 + line4 + line5 + line6 + line7

userid=[1,2,3,4,5,6,7,8,9]

您可以喜欢这段代码

def get_lines():
    line1 = "Document Header - Once per document"
    line2 = "\nDocument Information - Once per document"
    line3 = "\nDocument Information 2 - Once per document"
    line4 = "\nUser information 1"
    line5 = "\nUser Information 1"
    line6 = "\nUser Information 1"
    line7 = "\nDocument Footer - Once per document"

    return setLine(line1, 1) + setLine(line2, 1) + setLine(line3, 1) + setLine(line4, 3)+ setLine(line4, 3, "1", "2")


def setLine(content, iNum = 1, oldStr="", newStr=""):
    strStr = ""
    for ii in range(0, iNum):
    strStr += content.replace(oldStr, newStr)
    return strStr

print(get_lines())
示例代码输出为:

Document Header - Once per document
Document Information - Once per document
Document Information 2 - Once per document
User information 1
User information 1
User information 1
User information 2
User information 2
User information 2

为什么每行都有单独的变量?创建一个
行列表
;使用
random
module@JonClements是的,第4、5、6行将被链接,但每一行都是它自己的独立变量。我将使用示例片段进行编辑这是我当前从
get\u lines
方法获得的输出。