0

我正在从文件中切出一大块文本。现在块存储在一个变量中。喜欢,

str="from child hood days i liked sweet and savoir. in the early days my mother cooked great deal of sweet meats in home and all my relatives had a nice taste for good sweets. even in remote assam town we thronged miles to procure good sweets. and my sweet eating habit was pretty known. in my childhood i could had as good as 30 to 40 rosgollas at one seating after a full course of lunch. though i was not among the best in my family."

但是我现在想以与我们编写时相同的方式读取每一行

for line in file:
     print line

我的问题是我们可以这样做,还是应该写回文件然后执行操作?如果有人可以提供帮助。

问候, Subhabrata Banerjee Gurgaon India

4

3 回答 3

1
for line in str.split('\n'):
    doit(line)

虽然这不是懒惰地阅读解析。对于非常大的块,懒惰地读取解析:

for match in re.finditer(r'(.*)\n', str):
    print match.group(1)
于 2012-07-05T15:57:48.407 回答
1

将字符串转储到 a 中StringIO.StringIO,然后您可以对其使用文件操作。请注意,您的示例文本中只有一行。

于 2012-07-05T15:44:27.730 回答
0

也许使用字符串标记器通过将其拆分为“较小的字符串”来处理“大字符串”。这将使该过程变得容易得多。顺便说一句,你在编码什么?

于 2012-07-05T15:51:38.420 回答