Question

Using python3, how do i convert a csv file to an xlsx file?

Using python3,

how do i convert a csv file to an xlsx file?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CodeToCopy:

Note:

For installing pandas module, use this command in linux

$ sudo pip3 install pandas

csv_to_xlsx.py

# importing pandas module as pd

import pandas as pd

import sys

numArgs = len(sys.argv)

# checking number of command line arguments

# 1st argument -- input file path

# 2nd argument -- optional output file path; by default output file will be 'output.xlsx'

# 3rd argument -- delimiter between two fields; by defualt comma(,) will be a delimiter

if numArgs is 1 or numArgs > 4:

    print("Usage: " + sys.argv[0] + " <input csv file> [output xlsx file path] [field_delimiter]")

    exit(1)

# initializing variables with default variables

filepath_in = ""

filepath_out = "output.xlsx"

delimiter = ","

# taking input file path

if numArgs >= 2:

    filepath_in = sys.argv[1]

   

# taking output file path

if numArgs >= 3:

    filepath_out = sys.argv[2]

# taking field delimiter   

if numArgs is 4:

    delimiter = sys.argv[3]

try:

    # converting csv file into xlsx file

    pd.read_csv(filepath_in, delimiter).to_excel(filepath_out, index=False)

except Exception as e:

    print("Error: " + str(e))

       

CodeIndentation:

OutputScreenshot:

Add a comment
Know the answer?
Add Answer to:
Using python3, how do i convert a csv file to an xlsx file?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT