简体   繁体   中英

How to use command line argument in python dataframe?

I am working with CSV file and in my code, for every CSV file that I want to process, I should change the input file name manually and it is taking some times to do that every time.

My code looks as follow:

import pandas as pd
file = pd.read_csv('data_0.csv', error_bad_lines=False);

I want to use the command line argument, to make the process easier and enter any CSV file that I want as an input.

for example:

python code.py data_0.csv 

save by desire name. for example:

python code.py data_0.csv output_0.csv

Now, I already read many posts such as 1 , 2 , 3 , but I am not sure which one is faster and easier. Python documentation has many options such as sys.arg or argparse but I couldn't able to do it.

Thanks

sys.argv would work for this problem: https://www.pythonforbeginners.com/system/python-sys-argv

import pandas as pd
import gensim
import numpy as np
import string
from sys import argv

# read CSV input file
file = pd.read_csv(sys.argv[1], error_bad_lines=False);

"""" Here my code performs some actions! """"

# Save the output into CSV
df.to_csv(sys.argv[2], index=True, mode = 'a')

(where sys.argv[0] here being code.py)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM