简体   繁体   English

如何在逗号分隔的文件中显示逗号

[英]How to show a comma within a comma separated file

我正在使用php生成一个csv文件,现在某些列包含一个带逗号的段落,现在当我打开文件时,文件中的每个逗号都算作一个新列,是否有可能在某种程度上对这些逗号进行转义?

取决于您的CSV阅读器是什么,但“引用,应该工作”

Many CSV readers will allow commas within a single column by surrounding the column with double quotes. 许多CSV阅读器都会在单列中用双引号引起来以逗号。 In that case, double quotes can be represented by double double quotes: 在这种情况下,双引号可以用双双引号表示:

column 1,"column 2, with comma","column 3 with ""quote chars"", and comma"

That's the BIG problem with using a , in a CSV file. 这是一个大问题与使用,在一个CSV文件。 I would recommend using a different separator like | 我建议使用其他分隔符,例如| (it's less likely to appear on a text) or using a different more robust file format like XML for generating your file. (不太可能出现在文本上)或使用其他更健壮的文件格式(例如XML)来生成文件。

You're using a comma because it's a delimiter. 您使用逗号是因为它是分隔符。 That is, the comma has special meaning no matter when its used. 也就是说,无论何时使用逗号,它都有特殊的含义。 By that very definition, it becomes hard to treat it as context sensitive. 按照这种定义,很难将其视为上下文敏感的。 It can be done though, considering symbols like '\\n . 不过可以考虑考虑'\\n'\\n符号来完成。

You can try a new delimiter, such as ,\\n , though that might not be an option for you. 您可以尝试使用新的定界符,例如,\\n ,尽管这可能不是您的选择。 Looks to me that the best solution would be to use a different persistence mechanism. 在我看来,最好的解决方案是使用其他持久性机制。 Things will get sticky otherwise. 否则事情会变得很棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM