Reading in a file in io programming language - iolanguage

I'm looking to read in a simple text file using the IO language and print it to the screen,
so far I have:
f := File with("test.txt")
f openForReading
but just have no idea how to print it or clone the contents to an object. If anyone knows anything or could point me in a good direction it would be much appreciated.

Turns out it's very simple, just f contents. For any future reference to check for already existing methods for an object in io you can use protos, e.g. f protos

From the io> interactive shell, have you tried?
f print
or
doString(f)
See this blog

Use readLine to read one line to a string, and println to print.
f := File with(fileName)
f openForReading
l := f readLine
l println

Create a File object with your specified path:
fileName := "yourFileName.txt"
file := File with(fileName)
Open and read the file into a variable
file open
fileText := file readToEnd
Then close the file.
file close
You should then have the 'fileText' variable available for use.

Related

Write to file after match in Kotlin

New to Kotlin, I'd like to insert a line in the file after a specific match in the file. I know how to do this with sed like the following:
sed "/some line in file/a some text I'd like to add after line" file
But I'd like to understand how I'd go about this in Kotlin. So far I've got got as far as the printWriter interface, but I don't see anything that clearly implies an offset or regex parameter.
So far I've got:
File("file.txt").printWriter(...)
Thanks!
GNU 'sed' does not insert/remove/update lines in files, it transforms an input stream and provides options for sending the output stream to stdout, to a file, or even to a temporary file which then overwrites the original file after the transformation is complete (this is the --in-place option).
Here is some code that should get you started but note that there are lots of ways to buffer and read/write files, streams, etc.
val file = File("file.txt")
val tempFile = createTempFile()
val regex = Regex("""some line in file""")
tempFile.printWriter().use { writer ->
file.forEachLine { line ->
writer.println(when {
regex.matches(line) -> "a some text I'd like to add after line"
else -> line
})
}
}
check(file.delete() && tempFile.renameTo(file)) { "failed to replace file" }
See also sed, a stream editor for more details on how it transforms text streams.

Stata - efficiently appending 200+ files (my method takes hours)

I am trying to append approx. 200 files using Stata. Below I have provided the code I am using to append. The issue is that it is taking too long -- over 5 hours to do. The ultimate appended file has over 28 million observations and is about 2GB in size. I think the issue might be that it is saving every time and hence takes too long. I also tried using the tempfile mode -- but that also takes long. My colleague, on the other hand, did the same append in minutes using SAS. I have provided his code below as well. I would very much appreciate if someone could show me how to do it efficiently in Stata -- so that it would not take hours. Thanks much!
My Stata code:
file close _all
file open myfile using "$OP\filelist_test.txt", read
file read myfile line
cd "$OP"
insheet using "`line'", comma clear
tostring optionconditioncode, replace
save "$data\options_all", replace
file read myfile line
while r(eof)==0{
insheet using "`line'", comma clear
tostring optionconditioncode, replace
append using "$data\options_all"
save "$data\options_all", replace
file read myfile line
}
file close myfile
My colleague's SAS code:
data all_text (drop=fname);
length myfilename $100;
set dirlist;
filepath = "&dirname\"||fname;
infile dummy filevar = filepath length=reclen end=done missover dlm=',' firstobs=2 dsd;
do while(not done);
myfilename = filepath;
input var1
var2
var3
var4
output;
end;
Seems like the OP has not been around lately. The solution given by Robert Picard from the Stata forum link that the OP has provided is as follows:
> Take a look at -filelist- from SSC. It can create a Stata dataset of
> files (with full path). The help file has an example that does what
> you want efficiently. Here's a copy:
>
> use "csv_datasets.dta", clear
> local obs = _N
> forvalues i=1/`obs' {
> use "csv_datasets.dta" in `i', clear
> local f = dirname + "/" + filename
> insheet using "`f'", clear
> tempfile save`i'
> save "`save`i''"
> }
>
> use "`save1'", clear
> forvalues i=2/`obs' {
> append using "`save`i''"
> }

opening a batch file that opens a text file in python

I am writing a script that can execute a batch file, which needs to open a file in the same folder first. My current code is:
from subprocess import Popen
p = Popen("Mad8dl.bat <RUNTHISTO.txt>", cwd=r"C:\...\test")
stdout, stderr = p.communicate()
where the ... is just the path to the folder. However, everytime I run it I get the syntax error:
The syntax of the command is incorrect
Any help regarding the syntax would be greatly appreciated.
First, you should probably remove the < and > angle brackets from your code; just pass the filename, without any brackets, to your batch file. (Unless your filename really does contain < and > characters, in which case I really want to know how you managed it since those characters are forbidden in filenames in Windows).
Second, your code should look like:
from subprocess import Popen, PIPE
p = Popen(["Mad8dl.bat", "RUNTHISTOO.txt"], cwd=r"C:\...\test", stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
Note the list containing the components of the call, rather than a single string. Also note that you need to specify stdout=PIPE and stderr=PIPE in your Popen() call if you want to use communicate() later on.

Import password-protected xlsx workbook into R

How can I import a worksheet from a password-protected xlsx workbook into R?
I would like to be able to convert an Excel worksheet into a csv file without having to go through Excel itself.
It is possible for xls workbooks using the perl-based function xls2csv from package gdata. I gather that the problem is Spreadsheet::XLSX doesn't support it.
There are a variety of functions and packages for importing non-encrypted xlsx workbooks, but none seems to address this issue.
At present it seems the only alternatives are to go through Excel or figure out how to write perl code that can do it.
It looks to be what you need except it isn't with the xlsx package:
https://stat.ethz.ch/pipermail/r-help/2011-March/273678.html
library(RDCOMClient)
eApp <- COMCreate("Excel.Application")
wk <- eApp$Workbooks()$Open(Filename="your_file",Password="your_password")
tf <- tempfile()
wk$Sheets(1)$SaveAs(tf, 3)
To build on ed82's answer, there are a few caveats:
You may need to pass another password parameter, WriteResPassword. See docs here
I didn't find learning COM interface appealing after I got used to xlsx R package. So I would rather save a copy of the protected Excel file without a password immediately, close it, and read it in with another package:
eApp <- COMCreate("Excel.Application")
# Find out whether you need to pass **Password** or **WriteResPassword**
wk <- eApp$Workbooks()$Open(Filename= filename, Password="somepass", WriteResPassword = "somepass")
# Save a copy, clear the password (otherwise copy is still pass-protected)
wk$SaveAs(Filename = '...somepath...', WriteResPassword = '', Password = '')
# The copied file is still open by COM, so close it
wk$Close(SaveChanges = F)
# Now read into data.frame using a familiar package {xlsx}
my.data <- raed.xlsx('...somepath...', sheetIndex = ...)

Scala file reading adding spaces

I'm reading a file in scala using
def fileToString(that:String):String= {
var x:String=""
for(line <- Source.fromFile(that).getLines){
x += line + "\n"
}
x
}
This works fine for a scala file. But on a txt file it adds spaces between every character. For example. I read in a .txt file and get this:
C a l l E v e n t L o g ( E r r o r $ , E r r N u m , E r r O b j )
' E n d E r r o r h a n d l i n g b l o c k .
E n d S u b
and I read in the scala file for the program and it comes out normally
EDIT: It seems to be something to do with Encoding. When I change it to UTF-16, it reads the .txt file, but not the scala file. Is there a way to make it universally work?
No it can't work for all files. To read/interpret a file/data you need to know the format/encoding unless you're treating it as a binary blob.
Either save all files in the usual unicode format (UTF-8) or specify the encoding when reading the file.
FromFile takes an implicit codec, you can pass it explicitly.
io.Source.fromFile("123.txt")(io.Codec("UTF-16"))
In general, if you read from a file you need to know its encoding in order to correctly read the characters. I am not sure what the default encoding is that Scala assumes, probably UTF8, but you can either pass a Codec to fromFile, or specify the encoding as a string:
io.Source.fromFile("file.txt", "utf-8")
It's hard to be sure, but it sounds like the two files were written with different encodings. On any Unix system (including Mac) you can use the command od to look at the actual bytes in the file.
UTF-8 is the standard for ordinary text files on most systems, but if you have a mix of UTF-8 and UTF-16, you'll have to know which encoding to use for which files and correctly specify the encoding.
Or be more careful when you create the files to insure that they are all in the same format.