文件夹管理和文件读写——天下武功 唯快不破
召唤我们的歌神送出悲情一曲 ... ... ...
我等到花儿也谢了(Live) - live
专辑: 95友学友演唱会
歌手: 张学友
旁人来静静地看我到底哀伤等什么
旁人来回关心中安慰爱已没结果
热烈的开解一生等你也是奈何
仿佛我在拼命要稀罕援助
旁人来静静探听我昨天哪里出错
何时重逢迷失中飘起这首歌
是旧日是认真执着的某个故事
可惜这份信念到今天多么无助
曾天天真真的你
爱假想某日别离后
如孤孤单单的我
会否等你就似这歌
而飘飘忽忽的你
到真的决定别离后
留低空空虚虚的我
估不到结局是这歌
你知不知道你知不知道
我等到花儿也谢了
你知不知道你知不知道
我等到花儿也谢了
旁人来静静探听我昨天哪里出错
何时重逢迷失中飘起这首歌
是旧日是认真执着的某个故事
可惜这份信念到今天多么无助
曾天天真真的你
爱假想某日别离后
如孤孤单单的我
会否等你就似这歌
而飘飘忽忽的你
到真的决定别离后
留低空空虚虚的我
估不到结局是这歌
你知不知道你知不知道
我等到花儿也谢了
你知不知道你知不知道
我等到花儿也谢了
你知不知道你知不知道
我等到花儿也谢了
你知不知道你知不知道
我等到花儿也谢了
你知不知道你知不知道
我等到花儿也谢了
............................
你知不知道,你知不知道,我等到花儿也谢了~~~~~
前面我们专门探讨过“做则必成”,林语堂说过,人必有痴而后必成,你只要努力做你喜欢的事情,坚持坚持再坚持,总是有成功的时候。
这话听着挺有道理,但是这话听着也特别累。有很多人穷其一生坚持做自己(林语堂自己整一个中文打字机最后没整出来),结果也没有等到成功的那一天,坚持到身边的人都离开了,坚持到花儿也谢了,只好在孤独的夜晚打开收音机听歌神的这首情歌黯然神伤,满满的都是眼泪有没有?
坚持,真的那么简单吗?
坚持,往往意味着与世俗对抗,别低估世俗的力量,七大姑八大姨就是绳锯木断水滴石穿啊!
如果你的成功发生在5次失败之后,第6次你会成功,那在成功之前你所有的努力就是要加快这个尝试失败的过程。
速度,在这个时候往往会发挥非常大的作用!
最近入手了一台小黑Thinkpad New X1 Carbon,时尚的外表,CPU是i5的,固态硬盘,8G内存,续航时间可以达到8个小时 ... 尽管我现在花了好多银子,但是这些银子每天都以生产力的形式偿还给我,使我的速度得到了全面的提升,现在我工作每分钟都在收获它带来的好处。
有了新的笔记本,我可以一刻不停的在学校的任意的地方办公;我还可以一边在我的台式机跑数据,一边在我的笔记本上回邮件,感觉效率提升好几档;
我甚至爱上了敲键盘,打起字来身体每个毛孔都感觉特别顺畅滑溜,连错别字都快不认识我了呀( ⊙ o ⊙ )!
想想每次敲错字那种痛苦的体验吧,少点这样的体验意味着思路少打断一次,也意味着我们的眼睛从屏幕上转移到键盘上的时间都省下来了,哇哇不要太爽啊!
最近还升级了大屏幕27寸,还是曲面的,我的眼睛每次可以少转动一点角度,我可以同时在一个屏幕上有足够的地方把开发环境、浏览器和通信工具客户端同时摊开去这样每次可以少切换减少大脑因切换损耗的能量... ...
每天技能升级不停,每天的生产力投资多一点,每天都改进一点等有一天你突然回头,你会发现已经站在了普通人难以企及的高度。对“生产力”的投资它的回报总是很可观的!
你知不知道,你知不知道,我等到花儿也谢了~~~~~
我们每天都要给自己提速,早日变身闪电侠,在花儿谢了之前接住它把它放回去 O(∩_∩)O~
等有一天你的速度上去了之后能够给周围的人一起提速,改善他人的生活体验(活着不就是为了体验higher-level happiness),那你就不是速度了,你是"加"速度,到那种境界你这辈子就算没白活了。
看到有人留言,说“人生苦短,我用Python”,这是一个Matlab初学者教程,到底用哪种语言比较好?
我本人也接触过Python,Python的想象空间要大很多,尤其是涉及到Web这块。我以前听说过一个说法,说“Matlab是万能的”。其实Matlab不是万能的,它有一个特别明显的瓶颈:你不太可能在一个新的计算机上安装一个庞大的Matlab再去运行你开发的Matlab程序(Matlab的安装包貌似有几个G);这方面Python要好太多了,而且涉及到的网络方面Python要强很多,从这个意义上说Python才是万能的。
现在有一堆编程语言可以选,你选哪一种?
我个人推荐的选择方式是这样的:如果你想做学问搞研究(而且会有涉及到大量的数据分析工作和数据可视化的),推荐学习Matlab+VC++混合编程;如果你要赚钱,要赚好多好多钱,Python和Java会更适合。
速度是很重要的!编程的过程实际上是一个问题解决的过程,大部分的问题这个世界上的其他人都有可能遇到过,他们会在网络上分享他们的经验,Java在这方面做的是接近最好的,因为用Java的人是最多的,你在Java的世界里边遇到的问题,其他人遇到的概率就要大很多。这样你就可以直接借用之前的人的问题解决方案,而不用自己从底层开始摸索找解决办法,会节省很多时间。Python现在用的人也越来越多了,所以,Python今后也是一个大的趋势,推荐有抱负的同学学这个语言。其实这么说是不对的,对于没有抱负的同学,也要学Python,因为,因为它实在是太棒了!
现在我们来一起看看 Matlab版本从网络上抓图和Python版本从网络上抓图的程序代码:
Matlab版本:
%clear
clc; clear;
% get the Cell"wArray_Cell"
FileID_Wordlist = ['WordList_20130516.txt'];
FileID_Wordlist_youdaoMissed = ['WordList_youdaoMissed.txt'];
wArray_Cell = textread(FileID_Wordlist, '%s', 'delimiter', '\n');
% create a .txt file
tmpPathname = ['WordList_youdaoMissed.txt'];
fid = fopen(tmpPathname, 'w+');
fclose(fid);
% LOOP, index is b
for b = 1:length(wArray_Cell)
tmpWord = wArray_Cell{b};
Pathname_preString_ori = ['D:\Project_BigBang\downloading\Pictures_Original\'];
Pathname_preString_rev = ['D:\Project_BigBang\downloading\Pictures_Revised\'];
Pathname_postString = ['.jpg'];
pic_Pathname_ori = sprintf('%s%s%s', Pathname_preString_ori, tmpWord, Pathname_postString);
% download the target word from the internet www....
url_preString = ['http://dict.youdao.com/search?le=eng&q='];
url_postString = ['&keyfrom=dict.top'];
tmpUrl = sprintf('%s%s%s', url_preString, tmpWord, url_postString);
S = urlread(tmpUrl);
%%
StartMarker = ['<a><img src="'];
EndMarker = ['" alt="" data-link="'];
%
tmpNumStart = findstr(S,StartMarker) + 13;
tmpNumEnd = findstr(S,EndMarker) - 1;
%
url_JPG = S(tmpNumStart:tmpNumEnd);
if isempty(url_JPG) == 0
tmpV = urlwrite(url_JPG, pic_Pathname_ori);
else
tmpPathname = ['WordList_youdaoMissed.txt'];
fid = fopen(tmpPathname, 'a+');
fprintf(fid, '%s\r\n', tmpWord);
fclose(fid);
end
end
Python版本:
__author__ = 'psyJt'
# import zone
import os
import urllib
# const variables
preURL = 'http://pic.sogou.com/pics?query='
# get the current working path
# import os
CurrentWorkingPath = os.getcwd()
# get the txtPathName
txtFileName = 'WordsList.txt'
txtPathName = CurrentWorkingPath + '\\' + txtFileName
# open the txtPathName and read the words in it
fid = open(txtPathName)
strALL = fid.read()
fid.close()
# split the wordsList into single word list like ['apple', 'table', 'tiger']
tWords = strALL.split('\n');
# get the Html Source Codes
# import urllib
for singleWord in tWords:
# create new folder
# wordPath = CurrentWorkingPath + '\\' + singleWord
os.makedirs(singleWord)
tmpURL = preURL + singleWord
pHtml_ID = urllib.urlopen(tmpURL)
HtmlCodes = pHtml_ID.read()
pHtml_ID.close()
# process the HtmlCodes
# prepare the preJPG_Strings and the postJPG_Strings
preJPG_Strings = 'pic_url":"'
postJPG_Strings = '","similarUrlFirst'
nJPG_Strings = HtmlCodes.count(preJPG_Strings)
# prepare the imgList for the While LOOP
imgList = []
c = 1
i = 1
while c <= nJPG_Strings:
jpgStart = HtmlCodes.find(preJPG_Strings) + len(preJPG_Strings)
jpgEnd = HtmlCodes.find(postJPG_Strings)
jpgURL = HtmlCodes[jpgStart:jpgEnd]
HtmlCodes = HtmlCodes[jpgEnd + len(postJPG_Strings):]
imgList.append(jpgURL)
c += 1
# download the jpgURL into the iPathName
for i in range(nJPG_Strings):
picIndex = 100 + i
iFileName = "pic_%d.jpg" % (picIndex)
iPathName = CurrentWorkingPath + '\\' + singleWord + '\\' + iFileName
urllib.urlretrieve(imgList, iPathName)
由于我是先学习的Matlab,因此我的Python代码有非常深的Matlab烙印 O(∩_∩)O~
Matlab代码部分一开始要打开一个独立的.txt文件,我用的是一个叫做 textread() 的函数,这是继之前提到的 sprintf() 函数之后的又一明星函数。
我们来看看这个 textread() 函数的格式。
FileID_Wordlist = ['WordList_20130516.txt'];
wArray_Cell = textread(FileID_Wordlist, '%s', 'delimiter', '\n');
聪明的一休,您猜对了,这里只有一个
------
look
shot
opportunity
seize
want
capture
slip
palm
sweaty
knee
.
.
.
set
------
果然每行只有一个单词,聪明的一休,您果然很聪明!!
接下来,我们通过一个help命令查查这个 textread() 函数到底实现的是啥子功能?
>> help textread
textread Read formatted data from text file.
A = textread('FILENAME')
A = textread('FILENAME','',N)
A = textread('FILENAME','',param,value, ...)
A = textread('FILENAME','',N,param,value, ...) reads numeric data from
the file FILENAME into a single variable. If the file contains any
text data, an error is produced.
[A,B,C, ...] = textread('FILENAME','FORMAT')
[A,B,C, ...] = textread('FILENAME','FORMAT',N)
[A,B,C, ...] = textread('FILENAME','FORMAT',param,value, ...)
[A,B,C, ...] = textread('FILENAME','FORMAT',N,param,value, ...) reads
data from the file FILENAME into the variables A,B,C,etc. The type of
each return argument is given by the FORMAT string. The number of
return arguments must match the number of conversion specifiers in the
FORMAT string. If there are fewer fields in the file than in the
format string, an error is produced. See FORMAT STRINGS below for
more information.
If N is specified, the format string is reused N times. If N is -1 (or
not specified) textread reads the entire file.
If param,value pairs are supplied, user configurable options customize
the behavior of textread. See USER CONFIGURABLE OPTIONS below.
textread works by matching and converting groups of characters from the
file. An input field is defined as a string of non-whitespace
characters extending to the next whitespace or delimiter character
or until the field width is exhausted. Repeated delimiter characters
are significant while repeated whitespace characters are treated as
one.
FORMAT STRINGS
If the FORMAT string is empty, textread will only numeric data.
The FORMAT string can contain whitespace characters (which are
ignored), ordinary characters (which are expected to match the next
non-whitespace character in the input), or conversion specifications.
Supported conversion specifications:
%n - read a number - float or integer (returns double array)
%5n reads up to 5 digits or until next delimiter
%d - read a signed integer value (returns double array)
%5d reads up to 5 digits or until next delimiter
%u - read an integer value (returns double array)
%5u reads up to 5 digits or until next delimiter
%f - read a floating point value (returns double array)
%5f reads up to 5 digits or until next delimiter
%s - read a whitespace separated string (returns cellstr)
%5s reads up to 5 characters or until whitespace
%q - read a double-quoted string, ignoring the quotes (returns cellstr)
%5q reads up to 5 non-quote characters or until whitespace
%c - read character or whitespace (returns char array)
%5c reads up to 5 characters including whitespace
%[...] - reads characters matching characters between the
brackets until first non-matching character or
whitespace (returns cellstr)
use %[]...] to include ]
%5[...] reads up to 5 characters
%[^...] - reads characters not matching characters between the
brackets until first matching character or whitespace
(returns cellstr)
use %[^]...] to exclude ]
%5[^...] reads up to 5 characters
Note: Format strings are interpreted as with sprintf before parsing.
For example, textread('mydata.dat','%s\t') will search for a tab not
the character '\' followed by the character 't'. See the Language
Reference Guide or a C manual for complete details.
Using %* instead of % in a conversion causes textread to skip the
matching characters in the input (and no output is created for this
conversion).
The % can be followed by an optional field width to handle fixed
width fields. For example %5d reads a 5 digit integer. In
addition the %f format supports the form %<width>.<prec>f.
USER CONFIGURABLE OPTIONS
Possible param/value options are:
'bufsize' - maximum string length in bytes (default is 4095)
'commentstyle' - one of
'matlab' -- characters after % are ignored
'shell' -- characters after # are ignored
'c' -- characters between /* and */ are ignored
'c++' -- characters after // are ignored
'delimiter' - delimiter characters (default is none)
'emptyvalue' - empty cell value in delimited files (default is 0)
'endofline' - end of line character (default determined from file)
'expchars' - exponent characters (default is 'eEdD')
'headerlines' - number of lines at beginning of file to skip
'whitespace' - whitespace characters (default is ' \b\t')
textread is useful for reading text files with a known format. Both
fixed and free format files can be handled.
Examples:
Suppose the text file mydata.dat contains data in the following form:
Sally Type1 12.34 45 Yes
Joe Type2 23.54 60 No
Bill Type1 34.90 12 No
Read each column into a variable
[names,types,x,y,answer] = textread('mydata.dat','%s%s%f%d%s');
Read first column into a cell array (skipping rest of line)
[names]=textread('mydata.dat','%s%*[^\n]')
Read first character into char array (skipping rest of line)
[initials]=textread('mydata.dat','%c%*[^\n]')
Read file as a fixed format file while skipping the doubles
[names,types,y,answer] = textread('mydata.dat','%9c%5s%*f%2d%3s');
Read file and match Type literal
[names,typenum,x,y,answer]=textread('mydata.dat','%sType%d%f%d%s');
Read MATLAB file into cell array of strings
file = textread('fft.m','%s','delimiter','\n','whitespace','');
To read all numeric data from a delimited text file, use a single output
argument, empty format string, and the appropriate delimiter. For
example, suppose data.csv contains:
1,2,3,4
5,6,7,8
9,10,11,12
Read the whole matrix into a single variable:
[data] = textread('data.csv','','delimiter',',');
Read the first two columns into two variables:
[col1, col2] = textread('data.csv','%n%n%*[^\n]','delimiter',',');
For files with empty cells, use the emptyvalue parameter. Suppose
data.csv contains:
1,2,3,4,,6
7,8,9,,11,12
Read the file like this, using NaN in empty cells:
[data] = textread('data.csv','','delimiter',',','emptyvalue',NaN);
textread is not recommended. Use TEXTSCAN instead.
See also textscan, strread, dlmread, load, sscanf, xlsread.
Reference page in Help browser
doc textread
好多洋文啊,好多看不懂啊 T T
没关系,有我!!(不知道写轮眼还看不看我的帖子,看到的话会不会激动得泪流满面?)
这里有两个例子挺有特点的:
第一个例子
Examples:
Suppose the text file mydata.dat contains data in the following form:
Sally Type1 12.34 45 Yes
Joe Type2 23.54 60 No
Bill Type1 34.90 12 No
Read each column into a variable
[names,types,x,y,answer] = textread('mydata.dat','%s%s%f%d%s');
说明: .txt文件中的格式可以是一行多列,读取的时候可以返回多个变量(这个变量天然的是元胞数组类型的)
在这里之所以是元胞数组,而不是向量或者矩阵,是因为元胞数组是兼容并包的,从文本读取出来的信息,有可能是数值,字符串以及其他的任意类型,这适合用元胞数组来处理。
第二个例子
To read all numeric data from a delimited text file, use a single output
argument, empty format string, and the appropriate delimiter. For
example, suppose data.csv contains:
1,2,3,4
5,6,7,8
9,10,11,12
Read the whole matrix into a single variable:
[data] = textread('data.csv','','delimiter',',');
说明:textread() 函数读取的不一定是.txt文件,只要是ASCII(American Standard Code for Information Interchange)编码格式的文件都可以用 textread() 函数读取。这里 'delimiter' 后边跟的那个参数是间隔的字符(串),可以是空格啊 ' ' 也可以是制表符啊 ' ' (是不是很难区分出来,制表符和空格可以直接从.txt中拷贝出来,不管是哪种就不会有错了),例子中的间隔的字符是逗号',' 。
这样的话,我们再来看:
FileID_Wordlist = ['WordList_20130516.txt'];
wArray_Cell = textread(FileID_Wordlist, '%s', 'delimiter', '\n');
这应该解读为用 textread() 函数获取‘WordList_20130516.txt’文件中的单词列表并返回给wArray_Cell元胞类型的变量,读取的时候的间隔是'\n',也就是换行符的意思。
url_preString = ['http://dict.youdao.com/search?le=eng&q='];
url_postString = ['&keyfrom=dict.top'];
tmpUrl = sprintf('%s%s%s', url_preString, tmpWord, url_postString);
S = urlread(tmpUrl);
你从.txt文件中获取了单词列表之后,你要做的就是用sprintf()这个函数把某个单词,前边和后边套上特定的url字符串,然后用 urlread() 这个函数发送出去,并且把返回的源代码赋给 S 这个变量... ...
单词会变,其他的字符串信息和操作不变,就可以循环反复。
% LOOP, index is b
for b = 1:length(wArray_Cell)
end
这里的b就是我们循环递增的变量,每次加1,一直加到 wArray_Cell 单词列表的长度。
把源代码赋给S这个变量之后,要做的就是从里边找出图片对应的 url地址的字符串,然后用urlwrite()函数把这个地址对应的图片下载下来,保存成特定文件名的jpg图片。这里的首尾字符串查找并且定位中间的url地址字符串属于有点高阶的内容了,就不详细说明了,有兴趣的同学可以深入研究下代码,还是非常有意思的。
Python部分的代码就不解释了,这是Matlab教程,就权当我这是在装B吧 O(∩_∩)O~