[sage-support] Re: How can you read a .txt/csv file in Sage?
Sometimes its convenient to use the DATA directory, which will be packaged with the worksheet if you save it (as a .sws file). So for example you could copy the file to the DATA directory doing something like: os.system('cp /full/path/to/list2.txt ' + DATA + 'list2.txt') and then f = open(DATA + 'list2.txt') should work, and would still work if you saved a worksheet copy and moved it to another computer. I am tempted to open a ticket in trac for some sort of read_csv command, which might behave like the following (NOTE: this is just wishful thinking at this point): data_list = read_csv('my_file.csv') # data_list would be a list of lists of row data, converted to numerical types if possible data_list = read_csv('my_file.csv', seperator = '\t') #split lines by a tab character instead data_list = read_csv('my_file.csv', fields = [int,int,string]) #perhaps useful to have more explicit field conversions Does something like that already exist? Seems very common as a task. Of course its not that hard to do ad-hoc, but it would make things more user-friendly for lots of people I think. Marshall Hampton On Feb 5, 10:10 pm, Fall In Love with Sage cs.losi...@gmail.com wrote: Thank you! It works now :) On Feb 5, 9:54 pm, David Joyner wdjoy...@gmail.com wrote: On Thu, Feb 5, 2009 at 2:48 PM, Fall In Love with Sage cs.losi...@gmail.com wrote: Thank you for your response! The problem is now in the following codes: 1. f = open(list2.txt) l1 = f.readline() ls1 = l1.split( ) l1 gives: Traceback (click to the left for traceback) ... IOError: [Errno 2] No such file or directory: 'list2.txt' Similarly, the eval function, eval(ls1), in (1) and the following code (2) causes the previous error. 2. f = open('list2.txt') varList = [x.split(' // ') for x in f.readlines()] I have the file list2.txt at SageRoot. Does it help to give it the abs path? f = open(/full/path/to/list2.txt) On Feb 5, 9:28 pm, David Joyner wdjoy...@gmail.com wrote: Possibly I'm not understanding your English. Python can read in any text file, for example csv. So, the answer to your question seems to be in the thread you cited. Also, if you want to know which directory Sageroot is, type SAGE_ROOT. For example: sage: SAGE_ROOT '/home/wdj/sagefiles/sage-3.3.alpha1' Hope this helps. On Thu, Feb 5, 2009 at 2:11 PM, Fall In Love with Sage cs.losi...@gmail.com wrote: My .txt/csv -file looks like in Vim: 1,4 2,5 3,6 I can have both filetypes. I am not sure, which one Sage supports. Open two columns in Sage: - I know that the file needs be at Sageroot. My Sage is installed at ~/ apps/Sage in Mac. Where in ~/apps/Sage do I need to put the file? This question is based on the following discussions: 1.http://groups.google.com/group/sage-support/browse_thread/thread/4600... 2.http://stackoverflow.com/questions/494116/how-can-you-move-a-list-fro... --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: How can you read a .txt/csv file in Sage?
Thanks Jason, I'm surprised this never showed up on my radar before. -Marshall On Feb 6, 12:45 pm, Jason Grout jason-s...@creativetrax.com wrote: Marshall Hampton wrote: Sometimes its convenient to use the DATA directory, which will be packaged with the worksheet if you save it (as a .sws file). So for example you could copy the file to the DATA directory doing something like: os.system('cp /full/path/to/list2.txt ' + DATA + 'list2.txt') and then f = open(DATA + 'list2.txt') should work, and would still work if you saved a worksheet copy and moved it to another computer. I am tempted to open a ticket in trac for some sort of read_csv command, which might behave like the following (NOTE: this is just wishful thinking at this point): data_list = read_csv('my_file.csv') # data_list would be a list of lists of row data, converted to numerical types if possible data_list = read_csv('my_file.csv', seperator = '\t') #split lines by a tab character instead data_list = read_csv('my_file.csv', fields = [int,int,string]) #perhaps useful to have more explicit field conversions Does something like that already exist? Seems very common as a task. Of course its not that hard to do ad-hoc, but it would make things more user-friendly for lots of people I think. Yes, python has a very nice CSV module. Seehttp://www.python.org/doc/2.5.2/lib/module-csv.html Thanks, Jason --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: How can you read a .txt/csv file in Sage?
Marshall Hampton wrote: Sometimes its convenient to use the DATA directory, which will be packaged with the worksheet if you save it (as a .sws file). So for example you could copy the file to the DATA directory doing something like: os.system('cp /full/path/to/list2.txt ' + DATA + 'list2.txt') and then f = open(DATA + 'list2.txt') should work, and would still work if you saved a worksheet copy and moved it to another computer. I am tempted to open a ticket in trac for some sort of read_csv command, which might behave like the following (NOTE: this is just wishful thinking at this point): data_list = read_csv('my_file.csv') # data_list would be a list of lists of row data, converted to numerical types if possible data_list = read_csv('my_file.csv', seperator = '\t') #split lines by a tab character instead data_list = read_csv('my_file.csv', fields = [int,int,string]) #perhaps useful to have more explicit field conversions Does something like that already exist? Seems very common as a task. Of course its not that hard to do ad-hoc, but it would make things more user-friendly for lots of people I think. Yes, python has a very nice CSV module. See http://www.python.org/doc/2.5.2/lib/module-csv.html Thanks, Jason --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: How can you read a .txt/csv file in Sage?
Possibly I'm not understanding your English. Python can read in any text file, for example csv. So, the answer to your question seems to be in the thread you cited. Also, if you want to know which directory Sageroot is, type SAGE_ROOT. For example: sage: SAGE_ROOT '/home/wdj/sagefiles/sage-3.3.alpha1' Hope this helps. On Thu, Feb 5, 2009 at 2:11 PM, Fall In Love with Sage cs.losi...@gmail.com wrote: My .txt/csv -file looks like in Vim: 1,4 2,5 3,6 I can have both filetypes. I am not sure, which one Sage supports. Open two columns in Sage: - I know that the file needs be at Sageroot. My Sage is installed at ~/ apps/Sage in Mac. Where in ~/apps/Sage do I need to put the file? This question is based on the following discussions: 1. http://groups.google.com/group/sage-support/browse_thread/thread/4600e491955f4960 2. http://stackoverflow.com/questions/494116/how-can-you-move-a-list-from-excel-to-sage/514835#514835 --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: How can you read a .txt/csv file in Sage?
Thank you for your response! The problem is now in the following codes: 1. f = open(list2.txt) l1 = f.readline() ls1 = l1.split( ) l1 gives: Traceback (click to the left for traceback) ... IOError: [Errno 2] No such file or directory: 'list2.txt' Similarly, the eval function, eval(ls1), in (1) and the following code (2) causes the previous error. 2. f = open('list2.txt') varList = [x.split(' // ') for x in f.readlines()] I have the file list2.txt at SageRoot. On Feb 5, 9:28 pm, David Joyner wdjoy...@gmail.com wrote: Possibly I'm not understanding your English. Python can read in any text file, for example csv. So, the answer to your question seems to be in the thread you cited. Also, if you want to know which directory Sageroot is, type SAGE_ROOT. For example: sage: SAGE_ROOT '/home/wdj/sagefiles/sage-3.3.alpha1' Hope this helps. On Thu, Feb 5, 2009 at 2:11 PM, Fall In Love with Sage cs.losi...@gmail.com wrote: My .txt/csv -file looks like in Vim: 1,4 2,5 3,6 I can have both filetypes. I am not sure, which one Sage supports. Open two columns in Sage: - I know that the file needs be at Sageroot. My Sage is installed at ~/ apps/Sage in Mac. Where in ~/apps/Sage do I need to put the file? This question is based on the following discussions: 1.http://groups.google.com/group/sage-support/browse_thread/thread/4600... 2.http://stackoverflow.com/questions/494116/how-can-you-move-a-list-fro... --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: How can you read a .txt/csv file in Sage?
On Thu, Feb 5, 2009 at 2:48 PM, Fall In Love with Sage cs.losi...@gmail.com wrote: Thank you for your response! The problem is now in the following codes: 1. f = open(list2.txt) l1 = f.readline() ls1 = l1.split( ) l1 gives: Traceback (click to the left for traceback) ... IOError: [Errno 2] No such file or directory: 'list2.txt' Similarly, the eval function, eval(ls1), in (1) and the following code (2) causes the previous error. 2. f = open('list2.txt') varList = [x.split(' // ') for x in f.readlines()] I have the file list2.txt at SageRoot. Does it help to give it the abs path? f = open(/full/path/to/list2.txt) On Feb 5, 9:28 pm, David Joyner wdjoy...@gmail.com wrote: Possibly I'm not understanding your English. Python can read in any text file, for example csv. So, the answer to your question seems to be in the thread you cited. Also, if you want to know which directory Sageroot is, type SAGE_ROOT. For example: sage: SAGE_ROOT '/home/wdj/sagefiles/sage-3.3.alpha1' Hope this helps. On Thu, Feb 5, 2009 at 2:11 PM, Fall In Love with Sage cs.losi...@gmail.com wrote: My .txt/csv -file looks like in Vim: 1,4 2,5 3,6 I can have both filetypes. I am not sure, which one Sage supports. Open two columns in Sage: - I know that the file needs be at Sageroot. My Sage is installed at ~/ apps/Sage in Mac. Where in ~/apps/Sage do I need to put the file? This question is based on the following discussions: 1.http://groups.google.com/group/sage-support/browse_thread/thread/4600... 2.http://stackoverflow.com/questions/494116/how-can-you-move-a-list-fro... --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: How can you read a .txt/csv file in Sage?
Thank you! It works now :) On Feb 5, 9:54 pm, David Joyner wdjoy...@gmail.com wrote: On Thu, Feb 5, 2009 at 2:48 PM, Fall In Love with Sage cs.losi...@gmail.com wrote: Thank you for your response! The problem is now in the following codes: 1. f = open(list2.txt) l1 = f.readline() ls1 = l1.split( ) l1 gives: Traceback (click to the left for traceback) ... IOError: [Errno 2] No such file or directory: 'list2.txt' Similarly, the eval function, eval(ls1), in (1) and the following code (2) causes the previous error. 2. f = open('list2.txt') varList = [x.split(' // ') for x in f.readlines()] I have the file list2.txt at SageRoot. Does it help to give it the abs path? f = open(/full/path/to/list2.txt) On Feb 5, 9:28 pm, David Joyner wdjoy...@gmail.com wrote: Possibly I'm not understanding your English. Python can read in any text file, for example csv. So, the answer to your question seems to be in the thread you cited. Also, if you want to know which directory Sageroot is, type SAGE_ROOT. For example: sage: SAGE_ROOT '/home/wdj/sagefiles/sage-3.3.alpha1' Hope this helps. On Thu, Feb 5, 2009 at 2:11 PM, Fall In Love with Sage cs.losi...@gmail.com wrote: My .txt/csv -file looks like in Vim: 1,4 2,5 3,6 I can have both filetypes. I am not sure, which one Sage supports. Open two columns in Sage: - I know that the file needs be at Sageroot. My Sage is installed at ~/ apps/Sage in Mac. Where in ~/apps/Sage do I need to put the file? This question is based on the following discussions: 1.http://groups.google.com/group/sage-support/browse_thread/thread/4600... 2.http://stackoverflow.com/questions/494116/how-can-you-move-a-list-fro... --~--~-~--~~~---~--~~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~--~~~~--~~--~--~---