1

I have code from here and I would like to use this python script in bash like:

python conversion.py sample.tsv sample.xlsx

python script:

import csv
from xlsxwriter.workbook import Workbook

# Add some command-line logic to read the file names.
tsv_file = 'sample.tsv'
xlsx_file = 'sample.xlsx'

# Create an XlsxWriter workbook object and add a worksheet.
workbook = Workbook(xlsx_file)
worksheet = workbook.add_worksheet()

# Create a TSV file reader.
tsv_reader = csv.reader(open(tsv_file, 'rb'), delimiter='\t')

# Read the row data from the TSV file and write it to the XLSX file.
for row, data in enumerate(tsv_reader):
    worksheet.write_row(row, 0, data)

# Close the XLSX file.
workbook.close()

How can I modify this python script. Sorry for the maybe stupid question, but I do not work with python.

1
  • 3
    import sys, tsv_file = sys.argv[1], xlsx_file = sys.argv[2]. Commented Aug 19, 2016 at 9:48

1 Answer 1

3

You can use sys.argv.

....

tsv_file = sys.argv[1]

xlsx_file = sys.argv[2]

...

You can run like:

main.py sample.tsv sample.xlsx

You also can check argparse package. It provides much fancy functions. It is basically a wraper of sys.argv.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thank you for reply - I have this error: Traceback (most recent call last): File "conversion_xlsx.py", line 5, in <module> tsv_file = sys.argv[1] NameError: name 'sys' is not defined, Any idea? Thank you so much.
I forgot import sys.. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.