2006年11月27日 星期一 18:23
大家好:
这是我原来遇到过的一个问题:一个文本文件,其中包含一系列数字(设为浮点
数),要用程序将之读出。
后来我用python解决,总算是可以了,用float("字符") 的形式,而且需要复杂的
去符号过程,如"\n"," "。
但是我今天用c++的标准库实现了一下,发现居然如此简单!只要几句话就行了,
而且格式化非常明了:
#include
#include
using namespace std;
int main()
{
ifstream infile("test.txt");
float fval;
while(infile>>fval)
{
cout<< fval;
}
}
我不明白,为什么python没有像std一样实现标准文件输入输出格式化?而只能操
作字符串呢?
2006年11月28日 星期二 08:26
python-chinese£¬ÄãºÃ Xiaobin Sun£¬xbsun在metarnet.com 2006-11-28 C£«£«Ò»°ã²»ÕâôÓá£Ð§ÂÊ»®²»À´¡£ ----- Original Message ----- From: ËÕÑÇ To: python-chinese Sent: 2006-11-27, 18:23:39 Subject: [python-chinese] ¹ØÓÚ¸ñʽ»¯ÊäÈëÊý×Ö ´ó¼ÒºÃ£º ÕâÊÇÎÒÔÀ´Óöµ½¹ýµÄÒ»¸öÎÊÌ⣺һ¸öÎı¾Îļþ£¬ÆäÖаüº¬Ò»ÏµÁÐÊý×Ö£¨ÉèΪ¸¡µã Êý£©£¬ÒªÓóÌÐò½«Ö®¶Á³ö¡£ ºóÀ´ÎÒÓÃpython½â¾ö£¬×ÜËãÊÇ¿ÉÒÔÁË£¬ÓÃfloat("×Ö·û") µÄÐÎʽ£¬¶øÇÒÐèÒª¸´ÔÓµÄ È¥·ûºÅ¹ý³Ì£¬Èç"\n"£¬" "¡£ µ«ÊÇÎÒ½ñÌìÓÃc++µÄ±ê×¼¿âʵÏÖÁËһϣ¬·¢ÏÖ¾ÓÈ»Èç´Ë¼òµ¥£¡Ö»Òª¼¸¾ä»°¾ÍÐÐÁË£¬ ¶øÇÒ¸ñʽ»¯·Ç³£Ã÷ÁË£º #include#include using namespace std; int main() { ifstream infile("test.txt"); float fval; while(infile>>fval) { cout<< fval; } } ÎÒ²»Ã÷°×£¬ÎªÊ²Ã´pythonûÓÐÏñstdÒ»ÑùʵÏÖ±ê×¼ÎļþÊäÈëÊä³ö¸ñʽ»¯£¿¶øÖ»Äܲ٠×÷×Ö·û´®ÄØ£¿ _______________________________________________ python-chinese Post: send python-chinese在lists.python.cn Subscribe: send subscribe to python-chinese-request在lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061128/5eb72776/attachment.html
2006年11月28日 星期二 09:17
for l in open('XXXX').readlines():
print float(l.strip())
你觉得这样的代码复杂?:)
2006/11/27, 苏亚 <su1981ya在163.com>:
>
> 大家好:
>
> 这是我原来遇到过的一个问题:一个文本文件,其中包含一系列数字(设为浮点
> 数),要用程序将之读出。
> 后来我用python解决,总算是可以了,用float("字符") 的形式,而且需要复杂的
> 去符号过程,如"\n"," "。
>
> 但是我今天用c++的标准库实现了一下,发现居然如此简单!只要几句话就行了,
> 而且格式化非常明了:
>
> #include
> #include
>
> using namespace std;
>
> int main()
> {
> ifstream infile("test.txt");
> float fval;
> while(infile>>fval)
> {
> cout<< fval;
> }
> }
>
> 我不明白,为什么python没有像std一样实现标准文件输入输出格式化?而只能操
> 作字符串呢?
>
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
--
茫茫人海,你是我的最爱
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20061128/e016bb8f/attachment.html
2006年11月28日 星期二 09:30
ºÇºÇ£¬coutºÍcinµÄÁ÷¶¨ÏòÆäʵ²¢²»·½±ã£¬ÉÔ¸´ÔÓÓÃ;»¹ÊÇÒª»Øµ½º¯Êýµ÷ÓÃÉÏ£¬¾³£ÎÒÈ̲»×¡¾Í·³öC±ê×¼¿âÀ´ÓÃÁË¡£ 2006/11/27, ËÕÑÇ <su1981ya在163.com>: > > ´ó¼ÒºÃ£º > > ÕâÊÇÎÒÔÀ´Óöµ½¹ýµÄÒ»¸öÎÊÌ⣺һ¸öÎı¾Îļþ£¬ÆäÖаüº¬Ò»ÏµÁÐÊý×Ö£¨ÉèΪ¸¡µã > Êý£©£¬ÒªÓóÌÐò½«Ö®¶Á³ö¡£ > ºóÀ´ÎÒÓÃpython½â¾ö£¬×ÜËãÊÇ¿ÉÒÔÁË£¬ÓÃfloat("×Ö·û") µÄÐÎʽ£¬¶øÇÒÐèÒª¸´Ô > È¥·ûºÅ¹ý³Ì£¬Èç"\n"£¬" "¡£ > > µ«ÊÇÎÒ½ñÌìÓÃc++µÄ±ê×¼¿âʵÏÖÁËһϣ¬·¢ÏÖ¾ÓÈ»Èç´Ë¼òµ¥£¡Ö»Òª¼¸¾ä»°¾ÍÐÐÁË£¬ > ¶øÇÒ¸ñʽ»¯·Ç³£Ã÷ÁË£º > > #include> #include > > using namespace std; > > int main() > { > ifstream infile("test.txt"); > float fval; > while(infile>>fval) > { > cout<< fval; > } > } > > ÎÒ²»Ã÷°×£¬ÎªÊ²Ã´pythonûÓÐÏñstdÒ»ÑùʵÏÖ±ê×¼ÎļþÊäÈëÊä³ö¸ñʽ»¯£¿¶øÖ»Äܲ٠> ×÷×Ö·û´®ÄØ£¿ > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- »¶Ó·ÃÎÊ£º http://blog.csdn.net/ccat ÁõöÎ March.Liu -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061128/5b6f6016/attachment.htm
2006年11月28日 星期二 09:37
大熊 写道:
> for l in open('XXXX').readlines():
> print float(l.strip())
>
> 你觉得这样的代码复杂?:)
谢谢,但你的代码并不能解决我的问题:每一行得数字都不止一个,还需要用
split分割一下,例如:
for l in open('XXXX').readlines():
for m in l.strip().split(' '):
if m != '':
print float(m)
实现了,但我觉得似乎不那么python
2006年11月28日 星期二 09:45
On 11/28/06, 苏亚 <su1981ya在163.com> wrote: > 大熊 写道: > > for l in open('XXXX').readlines(): > > print float(l.strip()) > > > > 你觉得这样的代码复杂?:) > 谢谢,但你的代码并不能解决我的问题:每一行得数字都不止一个,还需要用 > split分割一下,例如: > > for l in open('XXXX').readlines(): > for m in l.strip().split(' '): > if m != '': > print float(m) > > 实现了,但我觉得似乎不那么python > 可以这样写: for line in file('xxxx'): print [float(x) for x in line.split()] 这里我生成了一个list,因为要打印的话,转不转没有什么用,打印仍然是输出字符串。不过很奇怪,如果只是打印,而程序中又只是用'\n'和空格分隔的,有什么好处理的? -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2006年11月28日 星期二 09:58
limodou 写道: > On 11/28/06, 苏亚 <su1981ya在163.com> wrote: > >> 大熊 写道: >> >>> for l in open('XXXX').readlines(): >>> print float(l.strip()) >>> >>> 你觉得这样的代码复杂?:) >>> >> 谢谢,但你的代码并不能解决我的问题:每一行得数字都不止一个,还需要用 >> split分割一下,例如: >> >> for l in open('XXXX').readlines(): >> for m in l.strip().split(' '): >> if m != '': >> print float(m) >> >> 实现了,但我觉得似乎不那么python >> >> > 可以这样写: > > for line in file('xxxx'): > print [float(x) for x in line.split()] > > 这里我生成了一个list,因为要打印的话,转不转没有什么用,打印仍然是输出字符串。不过很奇怪,如果只是打印,而程序中又只是用'\n'和空格分隔的,有什么好处理的? > > 的确并不复杂,但我一直是python的鼓吹者,且一直以为python在应用上应该比c+ +简单很多。也许这个例子太简单了,两者很相近
2006年11月28日 星期二 10:02
On 11/28/06, 苏亚 <su1981ya在163.com> wrote: > limodou 写道: > > On 11/28/06, 苏亚 <su1981ya在163.com> wrote: > > > >> 大熊 写道: > >> > >>> for l in open('XXXX').readlines(): > >>> print float(l.strip()) > >>> > >>> 你觉得这样的代码复杂?:) > >>> > >> 谢谢,但你的代码并不能解决我的问题:每一行得数字都不止一个,还需要用 > >> split分割一下,例如: > >> > >> for l in open('XXXX').readlines(): > >> for m in l.strip().split(' '): > >> if m != '': > >> print float(m) > >> > >> 实现了,但我觉得似乎不那么python > >> > >> > > 可以这样写: > > > > for line in file('xxxx'): > > print [float(x) for x in line.split()] > > > > 这里我生成了一个list,因为要打印的话,转不转没有什么用,打印仍然是输出字符串。不过很奇怪,如果只是打印,而程序中又只是用'\n'和空格分隔的,有什么好处理的? > > > > > 的确并不复杂,但我一直是python的鼓吹者,且一直以为python在应用上应该比c+ > +简单很多。也许这个例子太简单了,两者很相近 > 从道理上说语法可能会简单一些,但应用并不一定会简单。一个复杂的应用,并不会因为不同的语言就使用应用本身变简单,只是可能在具体实现上会因为某些特性使得开发变得简单倒是有可能的。 -- I like python! UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad My Blog: http://www.donews.net/limodou
2006年11月28日 星期二 10:03
C++µÄ´úÂë±íÏÖÁ¦ÆäʵÊǷdz£¿Ö²ÀµÄ£¬Pythonδ±Ø¾ÍÔÚÿ¸öϸ½Ú¶¼ÄܱÈC++´úÂë¸ü¼ò½à£¬ËµPythonµÄ´úÂë¼ò½à£¬ÊÇÒ»¸öºê¹Û¸ÅÄî¡£ÓÐÐËȤÄã¿ÉÒÔ²ÎÕÕÒ»ÏÂBoost::PythonµÄsample£¬ÓÐЩC++·â×°´úÂë±È¶ÔÓ¦µÄPython´úÂ뻹Ҫ¼ò½à£¬ºÜÓÐÒâ˼¡£ ÿһÖÖ¼¼Êõ¶¼²»ÊǾ¡Éƾ¡ÃÀµÄ£º£©¡£ ÔÚ06-11-28£¬ËÕÑÇ <su1981ya在163.com> дµÀ£º > > > µÄÈ·²¢²»¸´ÔÓ£¬µ«ÎÒÒ»Ö±ÊÇpythonµÄ¹Ä´µÕߣ¬ÇÒÒ»Ö±ÒÔΪpythonÔÚÓ¦ÓÃÉÏÓ¦¸Ã±Èc+ > +¼òµ¥ºÜ¶à¡£Ò²ÐíÕâ¸öÀý×ÓÌ«¼òµ¥ÁË£¬Á½ÕߺÜÏà½ü > > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- »¶Ó·ÃÎÊ£º http://blog.csdn.net/ccat ÁõöÎ March.Liu -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061128/4c2e3df2/attachment.htm
2006年11月28日 星期二 10:31
苏亚 写道:
> 大熊 写道:
>
>> for l in open('XXXX').readlines():
>> print float(l.strip())
>>
>> 你觉得这样的代码复杂?:)
>>
> 谢谢,但你的代码并不能解决我的问题:每一行得数字都不止一个,还需要用
> split分割一下,例如:
>
> for l in open('XXXX').readlines():
> for m in l.strip().split(' '):
> if m != '':
> print float(m)
>
> 实现了,但我觉得似乎不那么python
>
要怎样才叫python?
没你那么复杂,一个语句就可以搞定的
res = [ float(i) for line in file('xxx') for i in line.split() ]
split()缺省的就是按空白字符切分,所以不需要strip和m!=‘’判断;
在考虑简单的同时应该考虑异常的处理,如下:
try:
res = [ float(i) for line in file('xxx') for i in line.split() ]
except IOError,err: #打开文件异常
#do something
except ValueError,err: #值转换异常
#do something
够python吧
如果你还不满意再给你一个方法:
import re
res = [float(i) for i in
re.findall("(\d+\.?\d*)",'\n'.join(open("xxx").readlines()),re.M)]
你可以在匹配模式里面定义任何你需要的数据格式
比如你输出的数为:
250,250,250.00
2.50e6
$250.00
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
2006年11月28日 星期二 10:46
是啊,如果用来计算1+1,那什么语言都是差不多的 2006/11/28, limodou <limodou在gmail.com>: > > On 11/28/06, 苏亚 <su1981ya在163.com> wrote: > > limodou 写道: > > > On 11/28/06, 苏亚 <su1981ya在163.com> wrote: > > > > > >> 大熊 写道: > > >> > > >>> for l in open('XXXX').readlines(): > > >>> print float(l.strip()) > > >>> > > >>> 你觉得这样的代码复杂?:) > > >>> > > >> 谢谢,但你的代码并不能解决我的问题:每一行得数字都不止一个,还需要用 > > >> split分割一下,例如: > > >> > > >> for l in open('XXXX').readlines(): > > >> for m in l.strip().split(' '): > > >> if m != '': > > >> print float(m) > > >> > > >> 实现了,但我觉得似乎不那么python > > >> > > >> > > > 可以这样写: > > > > > > for line in file('xxxx'): > > > print [float(x) for x in line.split()] > > > > > > > 这里我生成了一个list,因为要打印的话,转不转没有什么用,打印仍然是输出字符串。不过很奇怪,如果只是打印,而程序中又只是用'\n'和空格分隔的,有什么好处理的? > > > > > > > > 的确并不复杂,但我一直是python的鼓吹者,且一直以为python在应用上应该比c+ > > +简单很多。也许这个例子太简单了,两者很相近 > > > > 从道理上说语法可能会简单一些,但应用并不一定会简单。一个复杂的应用,并不会因为不同的语言就使用应用本身变简单,只是可能在具体实现上会因为某些特性使得开发变得简单倒是有可能的。 > > -- > I like python! > UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad > My Blog: http://www.donews.net/limodou > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese -- 茫茫人海,你是我的最爱 -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20061128/f7b73e95/attachment.html
2006年11月28日 星期二 14:48
amingsc 写道:
> 苏亚 写道:
>
>> 大熊 写道:
>>
>>
>>> for l in open('XXXX').readlines():
>>> print float(l.strip())
>>>
>>> 你觉得这样的代码复杂?:)
>>>
>>>
>> 谢谢,但你的代码并不能解决我的问题:每一行得数字都不止一个,还需要用
>> split分割一下,例如:
>>
>> for l in open('XXXX').readlines():
>> for m in l.strip().split(' '):
>> if m != '':
>> print float(m)
>>
>> 实现了,但我觉得似乎不那么python
>>
>>
> 要怎样才叫python?
> 没你那么复杂,一个语句就可以搞定的
> res = [ float(i) for line in file('xxx') for i in line.split() ]
>
> split()缺省的就是按空白字符切分,所以不需要strip和m!=‘’判断;
> 在考虑简单的同时应该考虑异常的处理,如下:
> try:
> res = [ float(i) for line in file('xxx') for i in line.split() ]
> except IOError,err: #打开文件异常
> #do something
> except ValueError,err: #值转换异常
> #do something
>
> 够python吧
> 如果你还不满意再给你一个方法:
> import re
> res = [float(i) for i in
> re.findall("(\d+\.?\d*)",'\n'.join(open("xxx").readlines()),re.M)]
> 你可以在匹配模式里面定义任何你需要的数据格式
> 比如你输出的数为:
> 250,250,250.00
> 2.50e6
> $250.00
>
呵呵,谢谢!大开眼戒了。不过你有点小失算:用strip的原因是它可以去掉每一
行的回车符。
2006年11月28日 星期二 20:21
lisp (+ 1 1) On 11/28/06, ´óÐÜ <bearsprite在gmail.com> wrote: > > Êǰ¡£¬Èç¹ûÓÃÀ´¼ÆËã1+1£¬ÄÇʲôÓïÑÔ¶¼ÊDz¶àµÄ > > 2006/11/28, limodou <limodou在gmail.com>: > > > > On 11/28/06, ËÕÑÇ <su1981ya在163.com> wrote: > > > limodou дµÀ: > > > > On 11/28/06, ËÕÑÇ <su1981ya在163.com> wrote: > > > > > > > >> ´óÐÜ Ð´µÀ: > > > >> > > > >>> for l in open('XXXX').readlines(): > > > >>> print float(l.strip()) > > > >>> > > > >>> Äã¾õµÃÕâÑùµÄ´úÂ븴ÔÓ£¿:) > > > >>> > > > >> лл£¬µ«ÄãµÄ´úÂë²¢²»Äܽâ¾öÎÒµÄÎÊÌ⣺ÿһÐеÃÊý×Ö¶¼²»Ö¹Ò»¸ö£¬»¹ÐèÒªÓà > > > >> split·Ö¸îһϣ¬ÀýÈ磺 > > > >> > > > >> for l in open('XXXX').readlines(): > > > >> for m in l.strip().split(' '): > > > >> if m != '': > > > >> print float(m) > > > >> > > > >> ʵÏÖÁË£¬µ«ÎÒ¾õµÃËÆºõ²»ÄÇôpython > > > >> > > > >> > > > > ¿ÉÒÔÕâÑùд£º > > > > > > > > for line in file('xxxx'): > > > > print [float(x) for x in line.split()] > > > > > > > > > > ÕâÀïÎÒÉú³ÉÁËÒ»¸ölist£¬ÒòΪҪ´òÓ¡µÄ»°£¬×ª²»×ªÃ»ÓÐʲôÓ㬴òÓ¡ÈÔÈ»ÊÇÊä³ö×Ö·û´®¡£²»¹ýºÜÆæ¹Ö£¬Èç¹ûÖ»ÊÇ´òÓ¡£¬¶ø³ÌÐòÖÐÓÖÖ»ÊÇÓÃ'\n'ºÍ¿Õ¸ñ·Ö¸ôµÄ£¬ÓÐʲôºÃ´¦ÀíµÄ£¿ > > > > > > > > > > > µÄÈ·²¢²»¸´ÔÓ£¬µ«ÎÒÒ»Ö±ÊÇpythonµÄ¹Ä´µÕߣ¬ÇÒÒ»Ö±ÒÔΪpythonÔÚÓ¦ÓÃÉÏÓ¦¸Ã±Èc+ > > > +¼òµ¥ºÜ¶à¡£Ò²ÐíÕâ¸öÀý×ÓÌ«¼òµ¥ÁË£¬Á½ÕߺÜÏà½ü > > > > > > > ´ÓµÀÀíÉÏ˵Óï·¨¿ÉÄÜ»á¼òµ¥Ò»Ð©£¬µ«Ó¦Óò¢²»Ò»¶¨»á¼òµ¥¡£Ò»¸ö¸´ÔÓµÄÓ¦Ó㬲¢²»»áÒòΪ²»Í¬µÄÓïÑÔ¾ÍʹÓÃÓ¦Óñ¾Éí±ä¼òµ¥£¬Ö»ÊÇ¿ÉÄÜÔÚ¾ßÌåʵÏÖÉÏ»áÒòÎªÄ³Ð©ÌØÐÔʹµÃ¿ª·¢±äµÃ¼òµ¥µ¹ÊÇÓпÉÄܵġ£ > > > > -- > > I like python! > > UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad > > My Blog: http://www.donews.net/limodou > > _______________________________________________ > > python-chinese > > Post: send python-chinese在lists.python.cn > > Subscribe: send subscribe to python-chinese-request在lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > -- > ããÈ˺££¬ÄãÊÇÎÒµÄ×î°® > _______________________________________________ > python-chinese > Post: send python-chinese在lists.python.cn > Subscribe: send subscribe to python-chinese-request在lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request在lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20061128/ae3e1ed2/attachment.htm
2006年11月29日 星期三 13:48
呵呵,碰上较真的了 在06-11-28,wang boqun <wang.iostream在gmail.com> 写道: > > lisp > (+ 1 1) > > On 11/28/06, 大熊 <bearsprite在gmail.com> wrote: > > > > 是啊,如果用来计算1+1,那什么语言都是差不多的 > > > > -- 茫茫人海,你是我的最爱 -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20061129/b305a72a/attachment.html
Zeuux © 2025
京ICP备05028076号