从命令行读取 (x, y) 对流并将修改后的对 (x, f(y)) 写入文件
reads a stream of (x, y) pairs from the command line and writes the modified pairs (x, f(y)) to a file
这是问题所在:
从命令行读取 (x, y) 对流。
修改 datatrans1.py 脚本,使其从命令行读取 (x, y) 对流并将修改后的对 (x, f(y)) 写入文件。新脚本的用法,这里叫做datatrans1b.py,应该是这样的:
这是命令行的输入:
python datatrans1b.py tmp.out 1.1 3 2.6 8.3 7 -0.1675
生成输出文件 tmp.out:
- 1.1 1.20983e+01
- 2.6 9.78918e+00
- 7 0.00000e+00
提示:运行通过for循环中的sys.argv数组并使用range函数
具有适当的起始索引和增量
以下是datatrans1.py原稿:
```
import sys, math
try:
infilename = sys.argv[1]
outfilename = sys.argv[2]
except:
print("Usage:", sys.argv[0], "infile outfile")
sys.exit(1)
ifile = open(infilename, 'r') # open file for reading
ofile = open(outfilename, 'w') # open file for writing
def myfunc(y):
if y >= 0.0:
return y ** 5 * math.exp(-y)
else:
return 0.0
```
逐行读取 ifile 并写出转换后的值:
```
for line in ifile:
pair = line.split()
x = float(pair[0])
y = float(pair[1])
fy = myfunc(y) # transform y value
ofile.write('hello' '%g %12.5e\n' % (x, fy))
ifile.close()
ofile.close()
```
关于如何修改上述代码以正确 运行 命令行参数并生成具有坐标对的 tmp.out 文件的任何线索都将非常有用
这应该可以解决问题:
import sys, math
try:
outfilename = sys.argv[1]
except:
print("Usage:", sys.argv[0], "outfile pairs")
sys.exit(1)
ofile = open(outfilename, 'w') # open file for writing
def myfunc(y):
if y >= 0.0:
return y ** 5 * math.exp(-y)
else:
return 0.0
# Loop through y values, using slices to start at position 3
# and get every second value
for i, y in enumerate(sys.argv[3::2]):
# The corresponding x value is the one before the selected y value
x = sys.argv[2:][i*2]
# Call myfunc with y, converting y from string to float.
fy = myfunc(float(y))
# Write output using f-strings
ofile.write(f'({x}, {fy})\n')
这是问题所在:
从命令行读取 (x, y) 对流。 修改 datatrans1.py 脚本,使其从命令行读取 (x, y) 对流并将修改后的对 (x, f(y)) 写入文件。新脚本的用法,这里叫做datatrans1b.py,应该是这样的:
这是命令行的输入:
python datatrans1b.py tmp.out 1.1 3 2.6 8.3 7 -0.1675
生成输出文件 tmp.out:
- 1.1 1.20983e+01
- 2.6 9.78918e+00
- 7 0.00000e+00
提示:运行通过for循环中的sys.argv数组并使用range函数 具有适当的起始索引和增量 以下是datatrans1.py原稿:
```
import sys, math
try:
infilename = sys.argv[1]
outfilename = sys.argv[2]
except:
print("Usage:", sys.argv[0], "infile outfile")
sys.exit(1)
ifile = open(infilename, 'r') # open file for reading
ofile = open(outfilename, 'w') # open file for writing
def myfunc(y):
if y >= 0.0:
return y ** 5 * math.exp(-y)
else:
return 0.0
```
逐行读取 ifile 并写出转换后的值:
```
for line in ifile:
pair = line.split()
x = float(pair[0])
y = float(pair[1])
fy = myfunc(y) # transform y value
ofile.write('hello' '%g %12.5e\n' % (x, fy))
ifile.close()
ofile.close()
```
关于如何修改上述代码以正确 运行 命令行参数并生成具有坐标对的 tmp.out 文件的任何线索都将非常有用
这应该可以解决问题:
import sys, math
try:
outfilename = sys.argv[1]
except:
print("Usage:", sys.argv[0], "outfile pairs")
sys.exit(1)
ofile = open(outfilename, 'w') # open file for writing
def myfunc(y):
if y >= 0.0:
return y ** 5 * math.exp(-y)
else:
return 0.0
# Loop through y values, using slices to start at position 3
# and get every second value
for i, y in enumerate(sys.argv[3::2]):
# The corresponding x value is the one before the selected y value
x = sys.argv[2:][i*2]
# Call myfunc with y, converting y from string to float.
fy = myfunc(float(y))
# Write output using f-strings
ofile.write(f'({x}, {fy})\n')