473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

add new field(column) to a file

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.

8 15596
bvdet
2,851 Expert Mod 2GB
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
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
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
2,851 Expert Mod 2GB
@mari2025
%s will type cast the numbers to str.
Apr 2 '13 #5
bvdet
2,851 Expert Mod 2GB
The formatted string would be:
"%s %s %s" % (item.strip(), 1, 2)
Apr 2 '13 #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
2,851 Expert Mod 2GB
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
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

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

Similar topics

5
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need...
3
by: Ken | last post by:
Is it possible to freeze a column/field on a continuous form? Ken
0
by: webguy | last post by:
I have a text file that has fixed fields. I know how to grab the first few values out of the string by declaring the pattern... Dim pattern As String = _...
3
by: rainwood | last post by:
I've been trying for quite some time. I have a datagrid with a dropdown working perfect. But, now I want to make the field required. (default value is set to 'pick a type' and I want to make sure...
2
by: phillip.s.powell | last post by:
SELECT s.id, s.first_name, s.last_name, IF(s.school_year_id = 0, s.school_year_other, y.school_year_alt_display) AS school_year_name FROM student s LEFT OUTER JOIN school_year y ON...
2
by: phillip.s.powell | last post by:
For some bizarre reason, each time I insert or update a row in my table `image`, the field value for "image_width" is fixed at 127, no matter what value I use! Here is the schema: CREATE...
1
by: JB | last post by:
I'm dynamically creating a Gridview object and filling it with contents from an ArrayList as follows: GridView2.AutoGenerateColumns = true; GridView2.DataSource = ArrayListObject;...
9
by: evan1979.2 | last post by:
I am creating a new boolean column in a MSAccess .mdb database like this: Dim Cmd As New OleDb.OleDbCommand("ALTER TABLE m_table ADD boolColumn YesNo", objConn) Cmd.ExecuteNonQuery() , which...
2
by: RonMcIntire | last post by:
I wish to concatenate a column of email addresses in an access table or query into one string so I can cut and paste the string into the TO: field in an email. I'm using Access 2003. Can it be...
5
by: Moah Scout | last post by:
Hi, I have a Select query from two tables, I want in this query to add new column so that I can enter a new data in that column directly. Under normal situations I know its impossiple, but with...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.