Reputation: 121
I am slicing out a big chunk of text from a file. Now the chunk is stored in a variable. Like,
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."
But I now want to read each line in the same way as we write as
for line in file:
print line
My question is can we do this, or should I write back to the file and then do the operation? If any one can kindly help.
Regards, Subhabrata Banerjee Gurgaon India
Upvotes: 0
Views: 124
Reputation: 26501
for line in str.split('\n'):
doit(line)
Though that is not reading parsing lazily. For really big chunks read parse lazily:
for match in re.finditer(r'(.*)\n', str):
print match.group(1)
Upvotes: 1
Reputation: 2599
Maybe use a string tokenizer to handle the 'big string' by splitting it up into the 'smaller strings'. This will make the process a lot easier. By the way, what are you coding in?
Upvotes: 0
Reputation: 798746
Dump the string into a StringIO.StringIO
and then you can use file operations on it. Do note that your sample text only has one line in it though.
Upvotes: 1