replace characters
I use screenscraper to download meteo data (max and min temperature, wind) and save it in a csv file.
the script to save data is:
outputFile = session.getName() + ".csv";
try
{
File file = new File( outputFile );
fileExists = file.exists();
out = new FileWriter( outputFile, true );
session.log( "Writing data to a file." );
if (!fileExists)
{
// Write out the headers.
out.write("
out.write( "\n" ),
}
out.write( session.getVariable( "CITY " )+ "," );
out.write( session.getVariable( "DAY" )+ "," );
out.write( session.getVariable( "WIND" )+ "," );
out.write( session.getVariable( "MAX" )+ "," );
out.write( session.getVariable( "LOW" )+ "," );
out.write( "\n" );
out.close();
session.setVariable("WIND","");
session.setVariable("MAX","");
session.setVariable("LOW","");
session.setVariable("DAY","");
session.setVariable("CITY","");
}
catch( Exception e )
{
session.log( "An error occurred while writing the data to a file: " + e.getMessage() );
}
THE QUESTION IS: how can I replace the comma in the variable wind max low with a full stop (.) ?
I read other post and I see the function string.replaceAll but I don't understand how to use it?
can you help me? Could you tell me the exact script to replace characters?
thx a lot
Hi
Hi Fabio,
try:
session.setVariable("WIND",session.getVariable("WIND").replaceAll(",","."));
or the long way around which does the same thing:
String windString = session.getVariable("WIND");
windString = windString.replaceAll(",",".");
session.setVariable("WIND",windString);
I'm guessing you're doing this because you're writing to a CSV and the comma will mess up the file format. Alternately you could just surround the field with double quotes then you'd be ok if you had comma's in the actual data... i.e. say the value of WIND is 1,234
instead of:
out.write( session.getVariable( "WIND" )+ "," );
will write: 1,234,
try:
out.write("\"" + session.getVariable( "WIND" )+ "\"," );
which will write: "1,234",
thx
thx shadders!!!
session.setVariable("WIND",session.getVariable("WIND").replaceAll(",","."));
works very well!!
My last "replace problem" is a date format.
I scrape date in the following format DD/MM/YY
and I'd like to convert in YYYYMMDD
for example convert 09/06/09 in 20090609
could you suggest me the right script?
thx a lot
dealing with dates is a pain
dealing with dates is a pain in the proverbial. Try this:
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
Date date = formatter.parse(timeString);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyyMMdd");
String newTimeString = newFormat.format(date);
if you need to convert to different formats this page is a handy reference, you basically use the same code but with a different string representation of the format: http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html
cheers
Shadders, thx for your reply
Shadders,
thx for your reply but if my variable scraped scraper is DATE could you tell me the exact script?
I understand nothing in java, I really appreciate your patience and your support!!
Hi Fabio, for that script the
Hi Fabio,
for that script the input variable is 'timeString' so you just need to add one more line:
String timeString = session.getVariable("DATE");
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy");
Date date = formatter.parse(timeString);
SimpleDateFormat newFormat = new SimpleDateFormat("yyyyMMdd");
String newTimeString = newFormat.format(date);
the output variable 'newTimeString' contains the reformated date so you would use for your output so instead of something like:
out.write("\"" + session.getVariable( "DATE" )+ "\"," );
you would use something line:
out.write("\"" + newTimeString + "\"," );
I haven't tested this, if it doesn't work then add the following after the formatting part:
session.log("timeString: " + timeString);
session.log("newTimeString: " + newTimeString);
and post the results up here...
cheers
correction, yes I have tested
correction, yes I have tested it... You need to add one more line at the *beginning* of the script:
import java.text.SimpleDateFormat;
also one other thing to note, I'm not sure if you're using the normal date format ddMMyy or that strange American one where they put the month first and date second... The code above is for the non-american date format, if you want the american one you'll need to swap around the 'dd' and the 'MM'.
cheers
Shadder, I really appreciate
Shadder,
I really appreciate your patience and your support!!
thx thx thx
all script work perfectly thx again