首先准备一个example.csv文件,如下:
编写脚本test.py ,实现传入参数,读取example.csv文件,并将其保存为另一个文件,
# 含参数的脚本,读取一个文件,并另保存一个文件
import sys
in_file = sys.argv[1] # 第一个传入脚本的参数,读取文件
out_file = sys.argv[2] # 第二个传入脚本的参数,保存文件
with open("in_file", "r") as file_read:
with open("out_file", "w") as file_write:
titles = in_file.readline()
titles = titles.strip()
title_list = titles.split(',')
print(title_list)
file_write.write(','.join(map(str, title_list)) + '\n')
for row in file_read:
row = row.strip()
row_list = row.split(',')
print(row_list)
file_write(','.join(map(str, row_list))+'\n')
import sys
in_file = sys.argv[1] # 第一个传入脚本的参数,读取文件
out_file = sys.argv[2] # 第二个传入脚本的参数,保存文件
with open("in_file", "r") as file_read:
with open("out_file", "w") as file_write:
titles = in_file.readline()
titles = titles.strip()
title_list = titles.split(',')
print(title_list)
file_write.write(','.join(map(str, title_list)) + '\n')
for row in file_read:
row = row.strip()
row_list = row.split(',')
print(row_list)
file_write(','.join(map(str, row_list))+'\n')
执行脚本
python test.py example.csv wr.csv