By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,562 Members | 969 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,562 IT Pros & Developers. It's quick & easy.

add new field(column) to a file

P: 6
Hello,
I am beginner in Python and I need your guidance about the task I am given. I have to read a laser data file in python and then add two new columns to the data and fill them with specific integer values. I was able to read the file but was not successful in adding the new columns.I would be happy if you can kindly help me with it.
Expand|Select|Wrap|Line Numbers
  1. f = file("E:/SAGA/data/325125404.all", "r")
  2. >>> f.read()
  3. >>> w = file("E:/SAGA/data/325125404a.all", "w")
  4. >>> t=[]
  5. >>> for line in f.readlines():
  6.     t.append('\n',1)
  7.     w.writelines(t)
  8.     w.close()
Apr 2 '13 #1

✓ answered by bvdet

Everything written to a file must be a string. File contents are read as strings and must be type cast if another object type is required.

Share this Question
Share on Google+
8 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
Let's assume you want a comma delimited file, and you want to add two columns with the numbers 1 and 2 to each line. This is untested:
Expand|Select|Wrap|Line Numbers
  1. data = open("E:/SAGA/data/325125404.all", "r").readlines()
  2. output = ["%s,%s,%s" % (item.strip(), 1, 2) for item in data]
  3. f = open("E:/SAGA/data/325125404_modified.all", "w")
  4. f.write("\n".join(output))
  5. f.close()
I would suggest a different file name for the output, at least for testing.
Apr 2 '13 #2

P: 6
thank you very much for your prompt reply.
I tried it and works. Moreover,
if I want to add the new column using space between the new and the others what should I do?
Apr 2 '13 #3

P: 6
I also have question about "%s,", does not it use for the strings? as the 1 and 2 are numbers, should I use integer?
Apr 2 '13 #4

bvdet
Expert Mod 2.5K+
P: 2,851
@mari2025
%s will type cast the numbers to str.
Apr 2 '13 #5

bvdet
Expert Mod 2.5K+
P: 2,851
The formatted string would be:
"%s %s %s" % (item.strip(), 1, 2)
Apr 2 '13 #6

P: 6
I have changed your code a bit and now it shows what I really want:
Expand|Select|Wrap|Line Numbers
  1. data = open("E:/SAGA/data/325125404.all","r").readlines()
  2. output = ["%s\t%s" %(item.strip(),1) for item in data]
  3. f = open("E:/SAGA/data/325125404_mod.all","w")
  4. f.write("\n".join(output))
  5. f.close()
but still I am not sure that no where I have introduced the new column type as integer, or maybe I am wrong as I am beginner in Python. I would be thankful if you guide me about it.
Apr 2 '13 #7

bvdet
Expert Mod 2.5K+
P: 2,851
Everything written to a file must be a string. File contents are read as strings and must be type cast if another object type is required.
Apr 2 '13 #8

P: 6
thank you very much for your helpful guidance. I would like to ask another question. I am going to apply the above code for many files those are in one folder. in your view, what is the fastest approach, I was thinking I should create a function or is there any other methods for to do it as quick as possible?
Apr 3 '13 #9

Post your reply

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