![]() |
|
[Tut] Convert CSV to Excel xlsx in Python - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Convert CSV to Excel xlsx in Python (/thread-99592.html) |
[Tut] Convert CSV to Excel xlsx in Python - xSicKxBot - 06-17-2022 Convert CSV to Excel xlsx in Python 5/5 – (1 vote) Problem Formulation Challenge: Given a CSV file. How to convert it to an excel file in Python?![]() We create a folder with two files, the file Code: csv_to_excel.pyCode: my_file.csvCode: csv_to_excel.pyCode: my_file.csv![]() All methods discussed in this tutorial show different code snippets to put into Code: csv_to_excel.pyMethod 1: 5 Easy Steps in Pandas The most pythonic way to convert a Code: .csvCode: .xlsx
Code: import pandas as pd df = pd.read_csv('my_file.csv')
df.to_excel('my_file.xlsx', index=None, header=True)Note that there are many ways to customize the Code: to_excel()
If you want to do any of those, feel free to read our full guide on the Finxter blog here: Tutorial: Pandas Code: DataFrame.to_excel()Also, we’ve recorded a video on the ins and outs of this method here: Let’s have a look at an alternative to converting a CSV to an Excel file in Python: Method 2: Modules csv and openpyxl To convert a CSV to an Excel file, you can also use the following approach:
The following function converts a given CSV to an Excel file: Code: import csv
import openpyxl def csv_to_excel(csv_filename, excel_filename): # Read CSV file csv_data = [] with open(csv_filename) as f: csv_data = [row for row in csv.reader(f)] # Write to Excel file workbook = openpyxl.workbook.Workbook() worksheet = workbook.active for row in csv_data: worksheet.append(row) workbook.save(excel_filename) if __name__ == "__main__": csv_to_excel("my_file.csv", "my_file.xlsx")This is a bit more fine-granular approach and it allows you to modify each row in the code or even write additional details into the Excel worksheet. Where to Go From Here? Enough theory. Let’s get some practice! Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs? You build high-value coding skills by working on practical coding projects! Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people? If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home. Join the free webinar now! |