How to read text file in dolphindb? - database

The dolphindb manual says it can read file like
fin = file("C:/DolphinDB/test.txt")
x=fin.readLine()
But readLine() will return a string of this row.
I have a text file like:
1 2 3 4 5 6 7 8 9
How can I get these nine digits one by one ?

After you read the line, use the code below to convert
split(readLine(), ' ').int()

Related

Open file with formatted variable name in Julia

I have a list of files numbered gll_01.tab, gll_02.tab, ...., gll_20.tab in a subdirectory of my parent directory. These files are tabular data files.
I want to open/read files with user-specified input.
I can do:
a = 3
open("directory/gll_0$a.tab")
But using this approach, I would have to define two separate variable names for (01 to 09) and for (10 to 18). How can I use variables or strings with name 02, 03, ..., etc?
In python, I can have an equivalent command:
a = 4
g = '{:02d}'.format(a)
f = open('directory/gll_%s.tab' %g)
Is there an equivalent string formatting command in Julia?
A simple answer in this case would be to use lpad:
a = 3
open("directory/gll_$(lpad(a,2,"0")).tab")
If you need more fancy formatting you can use e.g. https://github.com/JuliaIO/Formatting.jl, in this case this would be:
using Formatting
a = 3
open("directory/gll_$(fmt("0>2", a)).tab")
Another option is to use #sprintf, docs are here. With that you can use %02d as a formatting option that would pad a digit d to length 2 with 0s preceding it:
julia> using Printf # this is in the standard library
julia> #sprintf("directory/gll_%02d.tab", 1)
"directory/gll_01.tab"
You can use this in your open statements too. Here they are in action:
julia> for i in 5:10
println("$i file is: $(#sprintf("directory/gll_%02d.tab",i))")
end
5 file is: directory/gll_05.tab
6 file is: directory/gll_06.tab
7 file is: directory/gll_07.tab
8 file is: directory/gll_08.tab
9 file is: directory/gll_09.tab
10 file is: directory/gll_10.tab

Convert tabulated column to array

My input is copied from a HTML table and looks like this in text format:
1 2 3
4 5 6
(imagine 'tabs' instead of the spaces)
The String would become then:
1\t\2\t3\r\n4\t5\t6
How can I create an array so that:
myArray(0,0) returns 1
myArray(0,1) returns 2
myArray(1,0) returns 4
I have tried this:
String input = Clipboard.GetText();
String[] content = input.Split(("\t").ToCharArray());
but this creates an array with the following elements:
1
2
3\r\n
4
5
6
-- Thank you... --
Since you know how to split it at the tabs, you can split it at the line break too.
stringIs = 1\t\2\t3\r\n4\t5\t6
firstSplit = stringIs.split("\r\n");
Now you have an array with two elements.
firstSplit[0] = "1\t\2\t3";
firstSplit[1] = "4\t5\t6";
So just split those the same way into a new array.

Python: How to compare 2 file text?

I have 1 large file text A and 1 small file text B. Now, I want compare file B and file A to see what is unique in file B.
For example:
File A:
1
2
3
4
5
File B
2
3
6
7
==> ouput
6
7
What is best solution for this ? I searched some thread in the website but i think my question is different because my file is large. Thanks
The below is my code but it doesn't work
with open('C:/unique.txt', 'wb') as out:
for line in open ('C:/B.txt'):
for line1 in open ( 'C:/A.txt' ):
if line != line1:
out.write(line)
I tried this and it worked for me. Hope this helps
with open('C:/unique.txt,'r+') as text:
with open('C:/unique2.txt','r+') as text2:
for read in text.readlines():
for read2 in text2.readlines():
if read2 not in read:
print(read2)

Printing lines of file without including newline in Python 2.7

I am trying to print the contents of a file. I have a file maze.txt with the following contents:
7 7
1 1 R N E
1 2 B N W
1 3 B N N
And I am printing it using the following code:
with open(os.path.join('maze.txt')) as f:
for line in f:
print line
f.close()
However, my output has extra empty lines in between:
7 7
1 1 R N E
1 2 B N W
1 3 B N N
I've tried changing my print line to print line[0:-1], which works except it will cut off the last character in the final line because there's not a newline to get rid of after it. Is there an easy way to avoid this?
Put a comma at the end of the print statement:
print line,
Just as the previous answer: when the print function doesn't end with a ',', then it adds a 'newline'.
Also, on your code, when opening a file with the 'with' code, you don't need to close the file: it's closed automatically when exiting the 'with' chunk of code.

Read from txt file to matrix after a specific expression

I wanted via matlab to read a table of data from a txt file after a specific expression and a number of non desired lines for example the AA.txt have:
Information about students :
AAAA
BBBB
1 10 100
2 3 15
! ! ! a number of lines
10 6 9
I have like information the expression 'Information about students', the number of skipped lines 2 and the number of columns 3 and rows 10 in desired matrix.
if I understand correctly, you wanna skip the first 3 lines (assuming them as headers) and then reading the rest.
I would follow this procedure:
fid = fopen(filename,'r');
A = textscan(fid,'%f %f %f','HeaderLines',3,'Delimiter','\r\n');
I currently do not have access to MATLAB, but I do believe it will work.

Resources