The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.
I'm trying to write a script to convert a csv into a tab delimited text file, but I'm running into trouble because some of the fields contain commas as regular punctuation.
I noticed that when the commas in the files in question were used as separators there were no spaces, and when they were used as punctuation there was always (as far as I can tell at first glance) the appropriate space after the comma. My thought was to replace all commas with tabs except commas followed by spaces, but I wasn't quite sure how to accomplish that. preg_replace doesn't seem to have a flag for exceptions to the replacement rule.
Unfortunately the file is coming from a client and dealing with the format is part of my job, so I'm stuck with commas as a delimiter.
Clam's method seems to mostly work, but it's creating a separate issue I can't really identify. Some fields are now starting with a comma (,N), not sure why.
Edit: The above problem occurs when a field is empty - i.e, ",," appears in the file.
That's assuming the person doing the "normal typing" is following proper typing procedure. It's been my experience that normal users don't know proper typing procedures. That is to say, you'll see stuff like this "hi there,i like you, it`s hot out today,."
Usually, with delimited files, text is required to be encased in quotes so that it's a tad bit easier to parse, otherwise it's going to be impossible.
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
Yeah this is an export from what I'm guessing is the client's shopping cart, but I'm not supposed to ask too many questions or they might get the impression that I don't know what I'm doing.
Something like this should work in PHP, if not exactly that. I haven't tested it, but you get the idea hopefully.
Tell them it's virtually impossible unless the free text has been sanitized to use something else or quoted properly so it can be parsed and modified to the new format. Otherwise you're probably boned. (My code requires something like this [121,Frank,"Hi There, I'm Frank!"])
bowen on
not a doctor, not a lawyer, examples I use may not be fully researched so don't take out of context plz, don't @ me
Posts
[PHP]
preg_replace('/,([^ ])/',"\t\\1",$text);
[/PHP]
search for a comma followed by not a space, and replace it with a tab followed by the same character.
Edit:
Clam's way should work too.
Clam's method seems to mostly work, but it's creating a separate issue I can't really identify. Some fields are now starting with a comma (,N), not sure why.
Edit: The above problem occurs when a field is empty - i.e, ",," appears in the file.
Usually, with delimited files, text is required to be encased in quotes so that it's a tad bit easier to parse, otherwise it's going to be impossible.
$areWeInQuotes = false;
foreach($text as &$char) {
if($char == ',' && !$areWeInQuotes)
$char = '\t';
else if($char == '"')
$areWeInQuotes = !$areWeInQuotes;
}
[/PHP]
Something like this should work in PHP, if not exactly that. I haven't tested it, but you get the idea hopefully.
Tell them it's virtually impossible unless the free text has been sanitized to use something else or quoted properly so it can be parsed and modified to the new format. Otherwise you're probably boned. (My code requires something like this [121,Frank,"Hi There, I'm Frank!"])
PHP has support to deal with this format for you in version 5. Take a look at
str_getcsv()