使用 Perl 从 Excel 中删除换行符或回车符
每当我尝试读取 .csv 文件中的一行时,它都会停止将该行视为单行并将其存储在多个数组中,就像假设的那样。我看到的是,当我读取 .csv 文件时,当我在记事本中打开它时,它似乎包含一些回车符/这些奇怪的方块。我想从文件中删除这些字符,以便我可以正确读取 .csv,而不会过早退出。我将如何使用 perl 脚本来完成此操作。
whenever I try a read a row in a .csv file it stops treating the row as a single row and stores it in multiple arrays, as supposed to just one. What i'm seeing is as I read a .csv file it seems to containg some carriage returns/ these weird squares when i open it in notepad. I want to remove these characters from the file so that i can properly read the .csv without it exiting too early. How would I go about doing this with a perl script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Perl 中的 chomp() 函数清理尾随空白。您可以使用类似的方法来删除所有尾随空格并将其替换为单个换行符:
You can clean up trailing whitespace with the chomp() function in perl. You can use something like this to chomp all trailing whitespace and replace it with a single newline:
如果您希望将整个文件作为一行读取,只需禁用输入记录分隔符即可:
也就是说,如果您希望使用 perl 内部的数据。如果您只是想更改输入文件以供其他程序使用,并且不关心换行符/回车符:
这将对
input.csv
进行就地编辑,并保存input.csv.bak
中的备份。请注意,如果运行此命令两次,备份将被覆盖,因此请在其他位置保存适当的备份。仅当您知道这些符号不应该出现时,我才会推荐这样做。
If you wish to read the whole file as one line, simply disable the input record separator:
That is, if you wish to use the data inside perl. If you simply wish to change the input file for other programs to use, and you do not care about the line feeds/carriage returns:
This will do an in-place edit of
input.csv
, and save a backup ininput.csv.bak
. Be aware that if you run this command twice, the backup is overwritten, so save a proper backup somewhere else.I would only recommend this if you know that these symbols should not be there.