Wednesday 15 September 2010

perl - How to deal with a comma in description in a CSV file -


Here's an interesting problem: I have a generic value file, which is imported with ID #, description and value. Which comes in the form of a comma, delimited file (CSV or TSV) from different vendors. One of the vendors uses a comma in their description field. The problem is in import that each comma defines a new column and closes the record. (It would be easy if the import file was fixed in length, but it is not regrettable.)

Question: Can anyone think about how to deal with the commas?

  id, description, value 1234, I want to change commas with a short-lived or hyphen, which would be acceptable.  

Good part, 1.23 2345, it is.ok, 2.34 3456, bad part, with commas, 4.56

In the first and second records, there are 3 columns As it should be. In the third example, this is the result of 4 columns and closes the import because it searches for the currency in the third column, but instead finds a string I am using Perl and Java script for the most part .

Citing the most common solution field can be "bad characters".

In this case:

  3456, "bad part, with a comma", 4.56   

and in return, if If you have an inward character, you avoid it (and therefore you do it with plain).

No comments:

Post a Comment