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.

Community
  • 1
  • 1
Geroge
  • 561
  • 6
  • 17

1 Answers1

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.

Luffy Cyliu
  • 811
  • 1
  • 9
  • 11
  • Hi, thank you for reply - I have this error: Traceback (most recent call last): File "conversion_xlsx.py", line 5, in tsv_file = sys.argv[1] NameError: name 'sys' is not defined, Any idea? Thank you so much. – Geroge Aug 19 '16 at 11:16
  • I forgot import sys.. Thank you so much. – Geroge Aug 19 '16 at 11:24