Python: How to compare 2 file text? - file

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)

Related

How to read text file in dolphindb?

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()

Writing my output into two columns in a text file .txt in C

I've looked around but couldn't find a satisfying solution... Basically I made a function that calculates the probability distribution of x number of loss in a portfolio of n credits... And I am trying to write the output in a text file into two columns where the first column would be the X (number of defaults) and second column would be the P(density function of each loss).. something like this:
X P
1 0.005
2 0.003
3 0.005
4 0.005
5 0.005
etc.
I've looked around and people suggested using negative- sign in front of my %d and %f when using fprintf but no luck....
Here's a sample of my code and the output it gives me...
Code:
for(i=0;i<d+1;i++)
{
Densite= gsl_ran_binomial_pdf(i,p,d);
fprintf(pF,"%-5d %-20f .\n",i, Densite);
}
Output:
0 0.005921 .
1 0.031161 .
2 0.081182 .
3 0.139576 .
4 0.178143 .
5 0.180018 .
6 0.150015 .
7 0.106026 .
8 0.064871 .
9 0.034901 .
10 0.016716 .
How to remedy?
Thanks in advance! (complete noob that started coding in C like two days ago..)
Did you run the executable program on Windows or Linux? If Window please use \r\n for new line.

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

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