在输入文件名后附加单词作为输出文件名 [英] Append word to input file name for output file name
问题描述
我正在尝试读取一些数据,处理这些数据,并将结果写到以原始文件名+单词"folded"保存的CSV中.我正在使用 sys.argv
传递输入文件名,并认为我可以创建一个新变量,例如 filename = sys.argv [1] +'_ folded.csv 代码>,但最终得到了
file.csv_folded.csv
.
I am trying to read in some data, process the data, and write the results to a CSV saved with original file name + the word "folded". I'm using sys.argv
to pass the input filename, and thought that I could just create a new variable such as filename = sys.argv[1]+'_folded.csv
but I ended up with file.csv_folded.csv
.
该如何将输出文件另存为file_folded.csv?
How can I do this so my output file is saved as file_folded.csv?
简化的代码示例:
import sys
import networkx as nx
G = nx.read_edgelist(sys.argv[1], delimitier=',')
fileout = sys.argv[1]+'_folded.csv'
nx.write_edgelist(G, fileout, delimiter=',')
推荐答案
您似乎已经在命令行参数中指定了 filename.csv
,因此您希望将其替换为特殊扩展名以可以做到:
It looks like you are already specifying filename.csv
in your command line argument so you want to replace that with your special extension to do that you could:
file_name = sys.argv[1].replace('.csv', '_folded.csv')
这篇关于在输入文件名后附加单词作为输出文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!