473,408 Members | 2,477 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,408 software developers and data experts.

Adding/Edditing a string in between line of a string

440 256MB
Hi

I would like to edit the string in a give line of string.

Expand|Select|Wrap|Line Numbers
  1. Sample
  2. >>> str1 = 'Pnt1    1000     1       2       3      '
  3. >>> str2 = str1[:8]
  4. >>> str3 = '2000    '
  5. >>> str4 = str2 + str3 + str1[16:40]
  6. >>> str4
  7. 'Pnt1    2000     1       2       3      '
  8.  
But for str3,I dont want to add the spaces at the tailing end knowing the filed format '8'.But I know the field width i.e '8' .Is it possible to add the spaces at the tailing end of the number,without counting the 'digits' and knowing the field format.

Any slicing mechanism or any other idea ,will help me

Thanks
PSB
Apr 13 '07 #1
2 1551
bvdet
2,851 Expert Mod 2GB
Hi

I would like to edit the string in a give line of string.

Expand|Select|Wrap|Line Numbers
  1. Sample
  2. >>> str1 = 'Pnt1    1000     1       2       3      '
  3. >>> str2 = str1[:8]
  4. >>> str3 = '2000    '
  5. >>> str4 = str2 + str3 + str1[16:40]
  6. >>> str4
  7. 'Pnt1    2000     1       2       3      '
  8.  
But for str3,I dont want to add the spaces at the tailing end knowing the filed format '8'.But I know the field width i.e '8' .Is it possible to add the spaces at the tailing end of the number,without counting the 'digits' and knowing the field format.

Any slicing mechanism or any other idea ,will help me

Thanks
PSB
??
Expand|Select|Wrap|Line Numbers
  1. >>> s = '%-8d' % 36507
  2. >>> s
  3. '36507   '
Apr 13 '07 #2
bvdet
2,851 Expert Mod 2GB
Expand|Select|Wrap|Line Numbers
  1. >>> s = 'Pnt1    1000     1       2       3      '
  2. >>> sList = [i for i in s.split() if i != '']
  3. >>> sList[1] = str(34567)
  4. >>> outStr = ''.join(['%-8s' % s for s in sList])
  5. >>> outStr
  6. 'Pnt1    34567   1       2       3       '
  7. >>> 
Apr 14 '07 #3

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

Similar topics

4
by: Garry | last post by:
Hi, I am new to python, hope someone can help me here: I have a MS Access exported .txt file which is tab delimited in total 20 columns, now I need to add another column of zero at the 4th column...
6
by: Mark | last post by:
Hi, My page has a table with many columns such that the right-side of the table gets chopped off when printed. I specify a table width of 100%, but otherwise no cell dimensions are specified. ...
6
by: 6thirty | last post by:
Hi, I've created a stocktaking database using Access XP. This is indexed by two fields - part number and shelf location. I am currently inputting all the data via a form. When I have entered a...
1
by: Brian Conklin | last post by:
Hello Eneryone, I am having a problem. I have written a little app that will take a text "pipe" delimited file and place all of the values in to an Excel spreadsheet. It works great on any of my...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
14
by: Paul_Madden via DotNetMonster.com | last post by:
Basically I have a listbox to which I add simple STRING items- I have a progress bar which I increment whenever I populate another portion of the complete set of items I wish to add. What I observe...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
0
by: AndyL69 | last post by:
Hello I've got a very strange Problem. When im adding a new ACE entry to a UNC Direcotry the inherented ACL's will be lost. When I'm adding a new ACE to a directory / file in this UNC path the...
1
telgroc
by: telgroc | last post by:
I have a string stored in a text file. I need to read the string into python as a list of dictionaries. The format of data in the text file (called "input.txt") is as follows: <first name>,<last...
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: 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
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...
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.