I have some code which appends text over an existing .txt file , from within R as following :
write("puts 'hellow world '", file = "C:\\Ruby22-x64\\bin\\elt.rb",
append = TRUE, sep = " ")
setwd("C:/Ruby22-x64/bin/")
test<-system("ruby elt.rb",intern=TRUE) # this will return "hellow world" from ruby interpreter
My question is: after appending the .rb file and running it, how can i remove the "puts 'hellow world '" string from the .rb file, and return it to its initial state?
I tried to look for many functions, but couldn't find any function which can undo the write function.
Just found out a wonderful gist that does the Job : https://gist.github.com/mages/1544009
gist:1544009
Related
I'm new to Scilab. I have to run the same program with a dozen different input files. Currently I simply uncomment the line and then rerun the program, and change the output file to a new name
// Input data file
data_file = 'data1.txt';
//data_file = 'data2.txt';
//data_file = 'data3.txt';
//data_file = 'data4.txt';
//data_file = 'data5.txt';
//data_file = 'data6.txt';
etc. another 6 lines
// Output data file name
output_data = '/output_files/data1.csv';
Is there a way to read in each file (data1.txt, data2.txt...) execute the body of the program and then output a new output file (data1.csv, data2.csv ...) instead of what I'm doing now, which is running the program and then editing it to use the next file and run again?
Just do something like:
for i=1:6
// Input data file
data_file=msprintf("data%d.txt",i);
// Output data file name
output_data=msprintf("/output_files/data%d.csv",i);
// exec the body of your script
end
I've written a very easy wrapper around GDAL in R. It utilises a prewritten statement which is passed to system, creating an output, which I then want to read into the R environment again.
It works by creates a temporary directory in the working directory, printing out an ESRI shape file of our area of interest, and then cuts a raster by this, with some preset information.
My problem: after successfully running the system() call and creating the output file, the function stops. It doesn't execute the next call and read the output into the R environment.
gdalwarpwarp <- function(source_file, source_srs, newfilename, reread=TRUE, clean=TRUE, cutline){
#Create tempfolder if it doesn't exist in the working directory.
if (!dir.exists("./tempfolder")){
dir.create("./tempfolder")
}
#Write temporary shape file
terra::writeVector(cutline, filename = "./tempfolder/outline_AOI.shp" , filetype='ESRI Shapefile',overwrite=TRUE)
#Warp!
if(reread==FALSE){
system(paste0("gdalwarp -cutline ./tempfolder/outline_AOI.shp -dstnodata -9999 -s_srs EPSG:3006 ",source_file, " ",paste0("./tempfolder/",newfilename)))
message('warp warped TRUE')
} else if(reread==TRUE){
system(paste0("gdalwarp -cutline ./tempfolder/outline_AOI.shp -dstnodata -9999 -s_srs EPSG:3006 ",source_file, " ",paste0("./tempfolder/",newfilename)))
newfilename <- terra::rast(paste0("./tempfolder/",newfilename))
}
}
This doesn't run:
newfilename <- terra::rast(paste0("./tempfolder/",newfilename))
The function did not return anything. Here is a somewhat improved version of your function. If you want to keep the output it would make more sense to provide a full path, rather then saving it to a temp folder. I also note that you are not using the argument source_srs
gdalwarpwarp <- function(source_file, source_srs, newfilename, reread=TRUE, clean=TRUE, cutline){
#Write temporary shape file
shpf <- file.path(tempdir(), "aoi.shp")
terra::writeVector(cutline, filename = shpf, filetype='ESRI Shapefile',overwrite=TRUE)
outf <- file.path(tempdir(), newfilename)
system(paste0("gdalwarp -cutline shpf -dstnodata -9999 -s_srs EPSG:3006 ",source_file, " ", outf)
if (reread) {
terra::rast(outf)
} else {
message('warp warped TRUE')
invisible(filename)
}
}
I wonder why you don't use terra::resample or terra::project; perhaps preceded or followed by mask (I may not understand the benefit of using cutline.
When using IPython's %%file magic to write the content of a notebook cell to a file in the current working directory, is there a way to suppress the Created file ... info text displayed on execution of the cell?
Sometimes creating files in this way is super handy (for example when using a Matlab kernel) but this is a huge problem with respect to version control, I don't want the structure of my local filesystem to be present in code that others work on as well.
source for this function
#cell_magic
def writefile(self, line, cell):
"""Write the contents of the cell to a file.
The file will be overwritten unless the -a (--append) flag is specified.
"""
args = magic_arguments.parse_argstring(self.writefile, line)
if re.match(r'^(\'.*\')|(".*")$', args.filename):
filename = os.path.expanduser(args.filename[1:-1])
else:
filename = os.path.expanduser(args.filename)
if os.path.exists(filename):
if args.append:
print("Appending to %s" % filename)
else:
print("Overwriting %s" % filename)
else:
print("Writing %s" % filename)
mode = 'a' if args.append else 'w'
with io.open(filename, mode, encoding='utf-8') as f:
f.write(cell)
File: /usr/local/lib/python3.6/dist-packages/IPython/core/magics/osm.py
I am trying to upload a CSV file that has special characters using ServletFileUpload of apache common. But the special characters present in the CSV are being stored as junk characters in the database. The special characters I have are Trademark, registered etc. Following is the code snippet.
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value "
+ Streams.asString(stream, "UTF-8") + " detected.");
}
}
I have tried reading it using BufferendReader, used request.setCharacterEncoding("UTF-8"), tried upload.setHeaderEncoding("UTF-8") and also checked with IOUtils.copy() method, but none of them worked.
Please advice how to get rid of this issue and where it needs to be addressed? Is there anything I need to do beyond servlet code?
Thanks
What database are using? What character set is database using? Characters can be malformed in the database rather than in Java code.
I need a way to open all the files in a folder, regardless of the filename. All the files have a .csv extension.
You could use listfiles and csvRead like this
function doSomethingWithCSVdata(CSV_data)
disp(CSV_data);
endfunction
csv_files = listfiles('*.csv');
for i=1:size(csv_files,1)
disp('Opening file: '+csv_files(i))
csvData = csvRead(csv_files(i),ascii(9), [], 'string');
doSomethingWithCSVdata(csvData);
end