使用python和perl语言实现xlsx转化为csv

下面提供使用 Python 和 Perl 两种常用语言的示例代码,来实现将 Excel 文件 (.xlsx) 转换为 CSV 文件。

首先是 Python 的示例代码:

使用 Python 实现 xlsx 转换为 csv:

# 导入所需模块
import pandas as pd

# 读取 Excel 文件并写入 CSV 文件
def convert_xlsx_to_csv(input_file, output_file):
    df = pd.read_excel(input_file)  # 读取 Excel 文件
    df.to_csv(output_file, index=False)  # 写入 CSV 文件

# 指定输入和输出文件名
input_excel_file = "input.xlsx"
output_csv_file = "output.csv"

# 执行转换
convert_xlsx_to_csv(input_excel_file, output_csv_file)

以上是使用 Python 完成 xlsx 转换为 csv 的示例,接下来是使用 Perl 的示例代码:

使用 Perl 实现 xlsx 转换为 csv:

# 导入所需模块
use strict;
use warnings;
use Spreadsheet::ParseXLSX;
use Text::CSV;

# 读取 Excel 文件并写入 CSV 文件
sub convert_xlsx_to_csv {
    my ($input_file, $output_file) = @_;

    my $parser = Spreadsheet::ParseXLSX->new();
    my $workbook = $parser->parse($input_file);

    die "Could not parse $input_file: $!" unless $workbook;

    my $csv = Text::CSV->new({binary => 1});
    open my $fh, ">:encoding(utf8)", $output_file or die "$output_file: $!";

    for my $worksheet ( $workbook->worksheets() ) {
        for my $row (0..$worksheet->maxrow) {
            $csv->combine($worksheet->row($row));
            print $fh $csv->string(), "\n";
        }
    }

    close $fh;
}

# 指定输入和输出文件名
my $input_excel_file = "input.xlsx";
my $output_csv_file = "output.csv";

# 执行转换
convert_xlsx_to_csv($input_excel_file, $output_csv_file);

您可以根据实际情况调整文件名和路径。这两个示例展示了分别使用 Python 和 Perl 实现 xlsx 到 csv 转换的方法。

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

芯作者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值