I have a txt plain file that has a list of numbers. One number per line, like:
978-972-722-649-8
978-972-722-646-7
978-972-722-627-6
978-972-722-625-2
978-972-722-594-1
etc.
This list is on a file called file.txt, was created and saved on TextEdit using Western (ISO Latin 1) encoding.
Now I am trying to use Applescript to read this file and transform it on a list.
I have this script
set myDirectory to path to desktop folder as string
set myFile to myDirectory & "file.txt"
try
set open_file to ¬
open for access file myFile without write permission
set myList to read open_file using delimiter return
close access open_file
on error
try
close access file open_file
end try
end try
after running this script, myList contains just ONE item and this item is "97" (I suppose that is the first two numbers of the first entry).
What is going on?
Thanks.
If you saved your file as Western (ISO Latin 1) most likely it has LF as a delimiter.
Try using the following line:
set myList to read open_file using delimiter "\n"
and see if it works for you.
I tried running your script on my laptop and it works fine with the above modifications.
Also, run the od command in the shell to see if the file is encoded right. Here is the output of my command:
vlad$ od -c file.txt
0000000 9 7 8 - 9 7 2 - 7 2 2 - 6 4 9 -
0000020 8 \n 9 7 8 - 9 7 2 - 7 2 2 - 6 4
0000040 6 - 7 \n 9 7 8 - 9 7 2 - 7 2 2 -
0000060 6 2 7 - 6 \n 9 7 8 - 9 7 2 - 7 2
0000100 2 - 6 2 5 - 2 \n 9 7 8 - 9 7 2 -
0000120 7 2 2 - 5 9 4 - 1 \n
0000132
It sounds like something weird is going on with the file, but have you tried another approach?
set myfilePPath to posix path of myFile
set myList to every paragraph of (do shell script("cat \"" & myfilePPath & "\""))
The cat programme just types out the contents of the file.
The read command's using delimiter(s) parameter is problematic. Use the following instead, which works with any linefeed character(s):
set myList to every paragraph of (read open_file)
Related
Currently, I have a text file and I'm interested in plotting two different curves from a single file(values for x axis are the same-column 1, values for y axis-columns 3 and 4). The plot should be in STDOUT since I'm working from ssh. The file that I am working with looks like this (filename: tmp)
%Iter duration train_objective valid_objective difference
0 6.0 0.0195735 0.0610958 0.0415223
1 5.0 0.180216 0.191344 0.011128
2 5.0 0.223318 0.241081 0.017763
3 6.0 0.245895 0.262197 0.016302
4 6.0 0.25796 0.28056 0.0226
5 6.0 0.269223 0.291769 0.022546
6 5.0 0.281187 0.298474 0.017287
7 5.0 0.283891 0.305579 0.021688
8 5.0 0.296456 0.307381 0.010925
9 5.0 0.296856 0.315487 0.018631
10 5.0 0.295805 0.321391 0.025586
Total training time is 0:06:27
So far, I can only plot the values corresponding to the 3rd column using the following line:
cat tmp | gnuplot -e "set terminal dumb size 120, 30; set autoscale; plot '-' u 1:3 with lines notitle"
Could someone tell me then how I could include the 4th column in the same plot? is that possible?
Thanks!
There is nothing in your description that rules out the trivial answer:
gnuplot -e "plot 'tmp' u 1:3 with lines, '' u 1:4 with lines"
The terminal choice is not relevant (you used 'set term dumb' but it could just as easily be any other output terminal, connection via ssh does not prevent that). If you have additional constraints that require a more complicated solution, please add them to the question.
In a directory in Windows I have 2 files, both of them with an accented character in its name: t1û.fn and t2ű.fn; The dir command in the Command Prompt shows both correctly:
S:\p>dir t*.fn
Volume in drive S is q
Volume Serial Number is 05A0-8823
Directory of S:\p
2017-09-03 14:54 4 t1û.fn
2017-09-03 14:54 4 t2ű.fn
2 File(s) 8 bytes
0 Dir(s) 19,110,621,184 bytes free
Screenshot:
However, Python can't see both files:
S:\p>python -c "import os; print [(fn, os.path.isfile(fn)) for fn in os.listdir('.') if fn.endswith('.fn')]"
[('t1\xfb.fn', True), ('t2u.fn', False)]
It looks like Python 2 uses a single-byte API for filenames, thus the accented character in t1û.fn is mapped to the single byte \xfb, and the accented character in t2ű.fn is mapped to the unaccented ASCII single byte u.
How is it possible to use a multi-byte API for filenames on Windows in Python 2? I want to open both files in the console version of Python 2 on Windows.
Use a unicode string:
f1 = open(u"t1\u00fb.fn") # t1û.fn
f2 = open(u"t2\u0171.fn") # t2ű.fn
I'm new to coding and am trying to iterate through folders to find a specific file (called the same thing in each folder). I have 3 folders (CONTROL, GROUP1, and GROUP2). Each folder has 2 subfolders in it from the folder names (2 3 4 5 6 7. Each subfolder has a file in it with the subfolder name such as 2_diff.nii or 3_diff.nii. I'd like the code to go into each folder, find the subfolders, and then perform an analysis of the file in there. The problem is that my code seems to be looking for each subfolder in each main folder and each main folder only has 2 of the subfolders out of (2 3 4 5 6 7). Any tips would be greatly appreciated - thank you!!
Folders=(CONTROL GROUP1 GROUP2)
SubFolders=(2 3 4 5 6 7)
data_source=/Users/sheena/Desktop/test/
for j in ${Folders[#]}; do
cd ${data_source}/${j}/
for i in ${SubFolders[#]}; do
fslroi ${i}_diff.nii ${i}_nodif 0 1 #I want to analyze the file <subfolder>_diff.nii and name the output as <subfolder>_nodif.nii
done
done
The way I understand your question is that in each of the directories CONTROL, GROUP1, and GROUP1 there are 2 files of the form x_diff.nii where x is a digit between 2 and 7. At least that's how I read your code.
You don't in advance know which two digits there are.
The easiest way I see it is to run through all possible SubFolders like you do, but use the continue statement early to jump to the next if it doesn't exist:
Folders=(CONTROL GROUP1 GROUP2)
SubFolders=(2 3 4 5 6 7)
data_source=/Users/sheena/Desktop/test/
for j in ${Folders[#]}; do
cd ${data_source}/${j}/
for i in ${SubFolders[#]}; do
if [[ ! -e ${i}_diff.nii ]]; then
continue
fi
fslroi ${i}_diff.nii ${i}_nodif 0 1
done
done
You could replace the if clause above with a single line:
for i in ${SubFolders[#]}; do
[[ -e ${i}_diff.nii ]] || continue
fslroi ${i}_diff.nii ${i}_nodif 0 1
done
But I find the more expressive if - fi block easier to read and understand, and that's important too.
This question already has answers here:
Inner join on two text files
(5 answers)
Closed 7 years ago.
I have two text files, one with the following content:
Brazil
China
USA
And another file with the following content:
Brazil 8 9 5 2
China 1 2 2 1
France 7 4 1 2
Italy 0 1 2 3
Spain 5 4 6 8
UK 8 5 4 1
USA 1 2 3 4
I would like to use some command that compares both files and returns
Brazil 8 9 5 2
China 1 2 2 1
USA 1 2 3 4
The command comm doesn't seem to work in cases like this. How could I solve it?
Edit: although this question is marked as duplicated, the solutions for the other question are not the same presented here.
Suppose you have nation names in file 1 and names and numbers in file 2 then
grep -wFf 1 2
should do the trick.
How can one get the version of Windows from the shell (command prompt) via a batch script for a drive that does not contain the active OS? I was hoping for some file that I could test, but it turns out things are a little more vague than I'd hoped. This should be able to determine the version of Windows for all NT releases from 2000 to 8.1.
you can load the windows registry from the "inactive OS' Drive" and read the version from it:
this is not tested, but it's something like this:
set SYSTEM_DRIVE=D:
reg load "HKU\ttt" "%SYSTEM_DRIVE%\Windows\System32\config\SOFTWARE"
reg query "HKU\ttt\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName
reg unload "HKU\ttt"
I would think a fairly robust method would be to look at the file version metadata for a standard OS file such as %SystemRoot%\system32\winver.exe.
I did the following (from command prompt):
'findstr /i "f.i.l.e.v.e.r.s.i.o.n" kernel32.dll > ver'
The dots are required because the metadata is unicode, and findstr will thus ignore the
zeros that are between the letters (dot being a wildcard for "any single character").
(see: findstr /?). The snippet can be examined in notepad or, on some systems that still support "EDIT", "EDIT /70 VER". The output is still unicode, but it can be "prettied up"
using programming (f/e vbscript). the raw material can be examined "as is":
F i l e V e r s i o n 6 . 1 . 7 6 0 1 . 1 9 1 3 5 ( w i n 7 s p 1 _ g d r . 1 6 0 1 2 1 - 1 7 1 8 ) 2. Googling windows versions yielded: me=4.9, 2000=5.0, xp=5.1, vista=6.0, 7=6.1, 8=6.2, 8.1=6.3, 10=10.0, (then a bunch more while microsoft put out fires, then 11=21H2. (7601 is the "build" number") Above taken from: https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions