471,350 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Best way to handle tabular data

I'm trying to read in a file line, change some of the line contents, and put the new contents back in the file to replace the old line. What's the best way to do this
Jun 11 '07 #1
5 1250
bartonc
6,596 Expert 4TB
I'm trying to read in a file line, change some of the line contents, and put the new contents back in the file to replace the old line. What's the best way to do this
There are many ways. A "best" way is up for interpretation. Let's see what you are trying to do and see if we can improve on it. That's how things work around here.

Thanks for joining.
Jun 11 '07 #2
Well I'm really looking for the easiest, not necessarily the best. I have a file that is just 100 rows of 10 columns of numbers seperated by spaces. I want to read in a row, increment one of the columns based on some input, and replace the line with the new data
Jun 11 '07 #3
bartonc
6,596 Expert 4TB
Well I'm really looking for the easiest, not necessarily the best. I have a file that is just 100 rows of 10 columns of numbers seperated by spaces. I want to read in a row, increment one of the columns based on some input, and replace the line with the new data
I'd say that the easiest thing would be to do all your manipulation in a 2 dimensional list, then use cpickle to store and retrieve the list from disk. The best way would be to use a true database.
Jun 11 '07 #4
Thanks for the suggestions, I got it.
Jun 11 '07 #5
bvdet
2,851 Expert Mod 2GB
Well I'm really looking for the easiest, not necessarily the best. I have a file that is just 100 rows of 10 columns of numbers seperated by spaces. I want to read in a row, increment one of the columns based on some input, and replace the line with the new data
Handling data stored this way is very straightforward. Pickling the data is a good way, but reading, updating, and writing back to file is also a good way:
Expand|Select|Wrap|Line Numbers
  1. fn = r'X:\dir\subdir\data.txt'
  2.  
  3. # Create a list of lists of integers
  4. dataList = [[int(i) for i in line.strip().split()] for line in file(fn).readlines()]
  5.  
  6. '''
  7. .....................................
  8. Manipulate your integer or float data here
  9. .....................................
  10. '''
  11.  
  12. f = file(fn, 'w')
  13. f.write('\n'.join([' '.join([str(i) for i in s]) for s in dataList]))
  14. f.close()
Jun 11 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

145 posts views Thread by Mark Johnson | last post: by
38 posts views Thread by Sanders Kaufman | last post: by
5 posts views Thread by Lars Eighner | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.