我如何才能将第一个进程(函数)的输出通过管道传输到 python 中的下一个进程(函数)?
How, can I pipe the output from the first process (function) to the next process (function) in python?
在下面的程序中
1) 我想将输出写入文件
2) 并且,还想通过管道传输文件的最终输出而不是从中读取来访问另一个进程下游的输出文件。
使用以下 python 代码:
global product
product = ""
product_file = open('product.txt', 'w')
def read_file():
file1 = open ('file01.txt', 'r')
data1 = read.file1().rstrip('\n')
data1_lines = data1.split('\n)
for lines in data1_lines:
columns = lines.split('\t')
x = int(columns[0])
y = int(columns[1])
prod = x*y
output = open('product.txt', 'a')
output.write(str(x) + '\t' + str(y) + '\t' + str(prod))
output.close()
product += str(x +'\t'+ y +'\t'+ prod +'\n')
def manipulate_file():
global product;
for lines in product:
line = product.split('\t')
do..something.....
我想访问 def read_file()
的最终输出文件以用于下游过程(函数即 def mainpulate_file()
),而不是再次打开它。
我想用subprocess.call
and/orstdin-out
and/ortempfile
,最后清空内存。
我阅读了几个示例,但找不到任何明确的过程。
如有任何解释,我将不胜感激。
如果您希望能够将当前写入 product.txt
的输出通过管道传输到其他进程,则必须将其写入标准输出。可以这样做:
line = str(x) + '\t' + str(y) + '\t' + str(prod)
output.write(line)
print(line, end='')
end=''
部分是为了防止在每个条目后打印换行符,因为您当前的代码也不会将它们写入文件。如果你真的想要换行符,你可以这样得到它们:
output.write(line + '\n')
print(line)
如果您实际上只想稍后在脚本中处理输出,您可以在进入循环之前创建列表并将每一行附加到该列表。在循环之后,您可以通过 '\n'.join(your_list)
.
连接所有行
我们甚至不需要 global
参数。
product = ""
product_file = open('product.txt', 'w')
def read_file():
file1 = open ('file01.txt', 'r')
data1 = read.file1().rstrip('\n')
data1_lines = data1.split('\n)
for lines in data1_lines:
columns = lines.split('\t')
x = int(columns[0])
y = int(columns[1])
prod = x*y
output = open('product.txt', 'a')
output.write(str(x) + '\t' + str(y) + '\t' + str(prod))
output.close()
product += str(x +'\t'+ y +'\t'+ prod +'\n')
return manipulate_file(product)
def manipulate_file(product):
data = StringIO(product)
data_lines = data.read().rstrip('\n).split('\n')
for lines in data_lines:
do..something.....
所以:
只需创建一个可以在循环中更新的变量即可。
Return 这个变量定义了函数
也使用返回的函数读取文件,但是需要使用StringIO
读取,因为数据在控制台上。
在下面的程序中
1) 我想将输出写入文件 2) 并且,还想通过管道传输文件的最终输出而不是从中读取来访问另一个进程下游的输出文件。
使用以下 python 代码:
global product
product = ""
product_file = open('product.txt', 'w')
def read_file():
file1 = open ('file01.txt', 'r')
data1 = read.file1().rstrip('\n')
data1_lines = data1.split('\n)
for lines in data1_lines:
columns = lines.split('\t')
x = int(columns[0])
y = int(columns[1])
prod = x*y
output = open('product.txt', 'a')
output.write(str(x) + '\t' + str(y) + '\t' + str(prod))
output.close()
product += str(x +'\t'+ y +'\t'+ prod +'\n')
def manipulate_file():
global product;
for lines in product:
line = product.split('\t')
do..something.....
我想访问 def read_file()
的最终输出文件以用于下游过程(函数即 def mainpulate_file()
),而不是再次打开它。
我想用subprocess.call
and/orstdin-out
and/ortempfile
,最后清空内存。
我阅读了几个示例,但找不到任何明确的过程。
如有任何解释,我将不胜感激。
如果您希望能够将当前写入 product.txt
的输出通过管道传输到其他进程,则必须将其写入标准输出。可以这样做:
line = str(x) + '\t' + str(y) + '\t' + str(prod)
output.write(line)
print(line, end='')
end=''
部分是为了防止在每个条目后打印换行符,因为您当前的代码也不会将它们写入文件。如果你真的想要换行符,你可以这样得到它们:
output.write(line + '\n')
print(line)
如果您实际上只想稍后在脚本中处理输出,您可以在进入循环之前创建列表并将每一行附加到该列表。在循环之后,您可以通过 '\n'.join(your_list)
.
我们甚至不需要 global
参数。
product = ""
product_file = open('product.txt', 'w')
def read_file():
file1 = open ('file01.txt', 'r')
data1 = read.file1().rstrip('\n')
data1_lines = data1.split('\n)
for lines in data1_lines:
columns = lines.split('\t')
x = int(columns[0])
y = int(columns[1])
prod = x*y
output = open('product.txt', 'a')
output.write(str(x) + '\t' + str(y) + '\t' + str(prod))
output.close()
product += str(x +'\t'+ y +'\t'+ prod +'\n')
return manipulate_file(product)
def manipulate_file(product):
data = StringIO(product)
data_lines = data.read().rstrip('\n).split('\n')
for lines in data_lines:
do..something.....
所以:
只需创建一个可以在循环中更新的变量即可。
Return 这个变量定义了函数
也使用返回的函数读取文件,但是需要使用
StringIO
读取,因为数据在控制台上。