473,387 Members | 1,791 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,387 software developers and data experts.

Add a new column to comma delimited text file.

Hi.. I have a file with two columns separated by comma with numerical values (sample) -
2305,11770
2306,12017
2307,9416.6
. , .
. , .
. , .

I willing to have an output file somewhat like this -
bfe,2305,hgen,,11770
bfe,2306,hgen,,12017
bfe,2307,hgen,,9416.6
and so on...

Thank you in advance...
Mar 14 '18 #1

✓ answered by Frinavale

There are a number of string manipulation methods that you can use in Python. Check out this quick reference for String Manipulation in Python.

What you could do is use the split method on each string you get from the file on the , character which would give you an array of 2 words.

Then you would make a new string by concatenating the values into the right positions.

For example:
Expand|Select|Wrap|Line Numbers
  1. oldString = "2305,11770"
  2. oldStringParts = oldString.Split(',')
  3. newString = "bfe," + oldStringParts[0] + ",hgen,," + olStringParts[1]
  4.  
There is probably a more elegant way of doing this with string replacement but I think this is the easiest to understand if you are new to programming.

1 1379
Frinavale
9,735 Expert Mod 8TB
There are a number of string manipulation methods that you can use in Python. Check out this quick reference for String Manipulation in Python.

What you could do is use the split method on each string you get from the file on the , character which would give you an array of 2 words.

Then you would make a new string by concatenating the values into the right positions.

For example:
Expand|Select|Wrap|Line Numbers
  1. oldString = "2305,11770"
  2. oldStringParts = oldString.Split(',')
  3. newString = "bfe," + oldStringParts[0] + ",hgen,," + olStringParts[1]
  4.  
There is probably a more elegant way of doing this with string replacement but I think this is the easiest to understand if you are new to programming.
Mar 14 '18 #2

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

Similar topics

1
by: Bernie Yaeger | last post by:
Is there a way to convert a .xml file to a comma delimited text file using vb .net? Thanks for any help. Bernie Yaeger
2
by: Kenneth Koski | last post by:
Hello All, I have a comma delimited text file, which I would like to move into a SQL 2000 table . I created a DTS package in SQL Server and saved it as a VB.bas . I am writting the code in C#...
4
by: John Tyce | last post by:
Does any one know how to write the data from a dataset table to a text file? I have found a clumsy way to do it using WriteXml, XmlReader, XmlNodeReader, etc, but this seems awful clumsy. Is there a...
0
by: JeffM | last post by:
If you've seen this before, sorry for the repost, the last post had no replies- and I'm still stuck. Can anyone suggest a way to use a comma delimited text file to supply arguements to a method...
3
by: Avi | last post by:
I need to create a text file that has the data from the 10 tables in the database. The number of fields in the tables exceeds 255 and so I cannot make a new table with all the fields and then...
4
by: JustSomeGuy | last post by:
Hi. I have a comma delimited text file that I want to parse. I was going to use fscanf from the C library but as my app is written in C++ I thought I'd use the std io stream library... My Text...
5
by: Mandrah | last post by:
I have a single text file with several lines of information with three items separated by commas. An example of a few lines: name1, email1, password1 name2, email2, password2 name3, email3,...
1
by: Quentin | last post by:
I want to take a comma delimited text file and export it to excel format, using the comma as the delimiter. Any help on this would be great, thank you.
4
by: Uncle Scotty | last post by:
When I use the File -Export function in Access, all it seems to do is duplicate the file. It doesn't seem to offer conversion to delimited text files. Can someone tell me how to do that kind of...
1
by: silpa | last post by:
I have a comma delimited text file which has data like this. 1, 11, "abc" ,False 2, 12, "def" ,False 3, 13, "ghi" ,False 4, 14, "jkl" ,False The name of this file is somedata.txt. Only one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.