Newbies FrankPottner Posted February 14, 2013 Newbies Posted February 14, 2013 Hi, I'm completely new to Groovy and am trying to replace the 3 versions of the double quotes with 2 single quotes in a large CSV file. I've managed to get the script below working but the input file encoding is UTF8 and the output file comes out as "Western (Mac OS Roman)". Any help would be greatly appreciated, Thanks File myInFile = new File( myFilePathStart ); myOutFile = new File( myFilePathEnd ); myOutFile.withWriter { out -> myInFile.eachLine { line -> myOutFileText = line.replaceAll(""", "''"); myOutFileText = myOutFileText.replaceAll("“", "''"); myOutFileText = myOutFileText.replaceAll("”", "''"); myOutFileText = myOutFileText + "n"; out << myOutFileText; } }; return true;
john renfrew Posted February 14, 2013 Posted February 14, 2013 myInFile = new File( myFilePathStart ) myOutFile = new File( myFilePathEnd ) try{ myOutFile.withWriter('UTF-8') { out -> myInFile.eachLine { line -> myOutFileText = line.replace(""", "''") .replace("“", "''") .replace("”", "''") + 'n' out << myOutFileText } //end eachLine } //end writer } catch (e){ return e } return true
Newbies FrankPottner Posted February 15, 2013 Author Newbies Posted February 15, 2013 Thanks "enthusiast with a bit of geek"... that almost worked. I just had to add the UTF8 encoding to the "myInFile.eachLine" command also. The script now looks like the following and does exactly what I need; myInFile = new File( myFilePathStart ) myOutFile = new File( myFilePathEnd ) try{ myOutFile.withWriter('UTF-8') { out -> myInFile.eachLine('UTF-8') { line -> myOutFileText = line.replaceAll(""", "''") .replaceAll("“", "''") .replaceAll("”", "''") + "n" out << myOutFileText; } //end eachLine } //end writer } catch (e) { return e } return true
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now