


How to convert Excel files to csv files using python script (code)
The content of this article is about how to convert Excel files to csv files (code) using python scripts. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
#!/usr/bin/env python __author__ = "lrtao2010" ''' Excel文件转csv文件脚本 需要将该脚本直接放到要转换的Excel文件同级目录下 支持xlsx 和 xls 格式 在同级目录下生成名为excel_to_csv.csv 的文件,采用UTF-8编码 ''' import xlrd import csv import os #生成的csv文件名 csv_file_name = 'excel_to_csv.csv' def get_excel_list(): #获取Excel文件列表 excel_file_list = [] file_list = os.listdir(os.getcwd()) for file_name in file_list: if file_name.endswith('xlsx') or file_name.endswith('xls'): excel_file_list.append(file_name) return excel_file_list def get_excel_header(excel_name_for_header): #获取表头,并将表头全部变为小写 workbook = xlrd.open_workbook(excel_name_for_header) table = workbook.sheet_by_index(0) #row_value = table.row_values(0) row_value = [i.lower() for i in table.row_values(0)] return row_value def read_excel(excel_name): #读取Excel文件每一行内容到一个列表中 workbook = xlrd.open_workbook(excel_name) table = workbook.sheet_by_index(0) #读取第一个sheet nrows = table.nrows ncols = table.ncols # 跳过表头,从第一行数据开始读 for rows_read in range(1,nrows): #每行的所有单元格内容组成一个列表 row_value = [] for cols_read in range(ncols): #获取单元格数据类型 ctype = table.cell(rows_read, cols_read).ctype #获取单元格数据 nu_str = table.cell(rows_read, cols_read).value #判断返回类型 # 0 empty,1 string, 2 number(都是浮点), 3 date, 4 boolean, 5 error #是2(浮点数)的要改为int if ctype == 2: nu_str = int(nu_str) row_value.append(nu_str) yield row_value def xlsx_to_csv(csv_file_name,row_value): #生成csv文件 with open(csv_file_name, 'a', encoding='utf-8',newline='') as f: #newline=''不加会多空行 write = csv.writer(f) write.writerow(row_value) if __name__ == '__main__': #获取Excel列表 excel_list = get_excel_list() #获取Excel表头并生成csv文件标题 xlsx_to_csv(csv_file_name,get_excel_header(excel_list[0])) #生成csv数据内容 for excel_name in excel_list: for row_value in read_excel(excel_name): xlsx_to_csv(csv_file_name,row_value) print('Excel文件转csv文件结束 ')
The above is the detailed content of How to convert Excel files to csv files using python script (code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Summary of some reasons why crontab scheduled tasks are not executed. Update time: January 9, 2019 09:34:57 Author: Hope on the field. This article mainly summarizes and introduces to you some reasons why crontab scheduled tasks are not executed. For everyone Solutions are given for each of the possible triggers, which have certain reference and learning value for colleagues who encounter this problem. Students in need can follow the editor to learn together. Preface: I have encountered some problems at work recently. The crontab scheduled task was not executed. Later, when I searched on the Internet, I found that the Internet mainly mentioned these five incentives: 1. The crond service is not started. Crontab is not a function of the Linux kernel, but relies on a cron.

Do you want to add some humor to your Python script or application? Whether you're building a chatbot, developing a command line tool, or just want to entertain yourself with random jokes, the pyjokes library can help. With pyjokes you can easily generate jokes in various categories and customize them to your liking. In this blog post, we will explore how to create random jokes in Python using the pyjokes library. We'll cover the installation process, generating different categories of jokes, customizing jokes, displaying them in a console application or web page, and handling any potential errors that may occur. Install pyjokes Before we start using pyjokes to create random jokes, we need

Python and Excel are two powerful tools that when combined can open up a world of automation. Python has versatile libraries and user-friendly syntax that enable us to write scripts to perform various tasks efficiently. Excel, on the other hand, is a widely used spreadsheet program that provides a familiar interface for data analysis and manipulation. In this tutorial, we will explore how to leverage Python to automate the process of refreshing Excel spreadsheets, saving us time and effort. Do you find yourself spending valuable time manually refreshing your Excel spreadsheet with updated data? This is a repetitive and time-consuming task that can really kill productivity. In this article we will guide you through using Py

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

Orange3 is a powerful open source data visualization and machine learning tool. It has rich data processing, analysis and modeling functions, providing users with simple and fast data mining and machine learning solutions. This article will briefly introduce the basic functions and usage of Orange3, and combine it with actual application scenarios and Python code cases to help readers better master the usage skills of Orange3. The basic functions of Orange3 include data loading, data preprocessing, feature selection, model establishment and evaluation, etc. Users can use the intuitive interface to drag and drop components to easily build data processes. At the same time, more complex data processing and modeling tasks can also be completed through Python scripts. Below we will go through a practical

In today's digital age, being aware of the latest changes on your website is crucial for a variety of purposes, such as tracking updates on your competitors' websites, monitoring product availability, or staying informed of important information. Manually checking your website for changes can be time-consuming and inefficient. This is where automation comes into play. In this blog post, we will explore how to create a Python script to monitor website changes. By leveraging the power of Python and some handy libraries, we can automate the process of retrieving website content, comparing it to previous versions, and notifying us of any changes. This allows us to remain proactive and react promptly to updates or modifications to the sites we monitor. Setting up the environment Before we start writing scripts to monitor website changes, we need to set up P

How to read Excel data using PyCharm? The steps are as follows: install the openpyxl library; import the openpyxl library; load the Excel workbook; access a specific worksheet; access cells in the worksheet; traverse rows and columns.

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.
