<div dir="ltr"><br><br><div class="gmail_quote">On Thu, Jul 31, 2008 at 8:07 AM, S Python <span dir="ltr"><<a href="mailto:spython01@gmail.com">spython01@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr"><div>Hi Everyone,</div>
<div> </div>
<div>I am trying to read a comma-delimitted list ("aaa","bbb","ccc") from a text file and assign those values to a list, x, such that:</div>
<div> </div>
<div>x = ["aaa", "bbb", "ccc"]</div>
<div> </div>
<div>The code that I have come up with looks like this:</div>
<div> </div>
<div>>>> x = []<br>>>> f = open(r'c:\test.txt', 'r')<br>>>> x.extend(f.readlines())<br>>>> x<br>['"aaa","bbb","ccc"']</div>
<div> </div>
<div>If you look closely, there is an extra pair of single quotes (') that encapsulates the string. Therefore, len(x) returns 1, instead of 3. Is there a function to "separate" this list out? I hope my question makes sense.</div>
<div> </div>
<div>Thanks in advance.</div>
<div> </div>
<div>Samir</div>
<div> </div></div></blockquote><div>This is an answer by a novice, and it may not be the best around;<br>Why don't you first get rid of the quotation marks and then split on the comma:<br><br>>>> f = open(r'c:\test.txt', 'r').read().replace('"', '')<br>
>>> x = []<br>>>> x.extend(f.split(","))<br>>>> x<br>['aa', ' bb', ' cc']<br>>>> len(x)<br>3<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div dir="ltr"><div></div>
<div> </div></div>
<br>_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.....محمد الغزالي<br>"No victim has ever been more repressed and alienated than the truth"<br>
<br>Emad Soliman Nawfal<br>Indiana University, Bloomington<br><a href="http://emnawfal.googlepages.com">http://emnawfal.googlepages.com</a><br>--------------------------------------------------------<br>
</div>