使用shell命令执行字符操作

发布于 2024-12-05 17:59:20 字数 612 浏览 1 评论 0原文

我需要将从 Mac Excel 2011 导出的 CSV 文件转换为 CMS 识别的可导入格式(解决方案不应相关,但导入格式适用于 Drupal Feeds 模块,尽管是目标)。

目前为了做到这一点,我需要在 Vim 中执行以下操作:

:%s/\r/\r/g
:w ++enc=utf8

这基本上意味着:

  1. 将回车符转换为某种通用格式
    • 最初在 Excel 导出它们时,回车符由 ^M 表示
    • Vim 命令 :%s/\r/\r/g 将它们全部转换为 CMS 识别为回车符的格式
  2. 将字符编码转换为 UTF8。
    • 最初导出时,字符集是 ASCII Extended 或类似的字符集。

理想情况下,需要在作为导入的一部分上传文件时触发该过程,这意味着 PHP 将触发该过程,无论这是否对该过程有任何影响。不过,此时我觉得将解决方案作为 shell 脚本或类似的东西来处理会更舒服,但是如果我能弄清楚如何将其挂接到 Drupal 7 Feeds 中,那么 PHP 解决方案当然是受欢迎的。

I need to convert a CSV file exported from Mac Excel 2011 to an importable format recognized by a CMS (the solution should not be related, however the import format is for Drupal Feeds module, although the target).

In order to do this currently I need to perform the following operations in Vim:

:%s/\r/\r/g
:w ++enc=utf8

Which basically means:

  1. Convert carriage returns to some sort of universal format
    • Initially as Excel exports them, the carriage return character is represented by ^M
    • the Vim command :%s/\r/\r/g converts them all to a format the CMS recognizes as a carriage return
  2. Convert the character encoding to UTF8.
    • As exported initially, the character set is ASCII Extended or something similar.

Ideally this process will need to be triggered upon uploading the file as part of the import, which means PHP will trigger the process, whether that has any bearing on the process. However I feel more comfortable at this point handling the solution as a shell script or something similar, but of course PHP solutions are welcome if I can figure out how to hook it into Drupal 7 Feeds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲墙角沉默 2024-12-12 17:59:20

一些未经测试的代码:

#!/bin/php
<?php

$replacements = array(
    // Adjust destination char to your liking
    "\r\n" => "\n",
    "\r" => "\n",
    "\n" => "\n",
);

// No risk to split chars: input is single byte
while( $line = fread(STDIN, 10240) ){
    // Normalize line feeds
    $line = strtr($line, $replacements);

    // Convert to UTF-8 (adjust source encoding to your needs)
    $line = iconv('CP1252', 'UTF-8', $line);

    fwrite(STDOUT, $line);
}

用法:

./fix-csv < input.csv > output.csv

Some untested code:

#!/bin/php
<?php

$replacements = array(
    // Adjust destination char to your liking
    "\r\n" => "\n",
    "\r" => "\n",
    "\n" => "\n",
);

// No risk to split chars: input is single byte
while( $line = fread(STDIN, 10240) ){
    // Normalize line feeds
    $line = strtr($line, $replacements);

    // Convert to UTF-8 (adjust source encoding to your needs)
    $line = iconv('CP1252', 'UTF-8', $line);

    fwrite(STDOUT, $line);
}

Usage:

./fix-csv < input.csv > output.csv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文