Python Forum
Connecting two string after spliting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Connecting two string after spliting
#1
I search for lines starts with '+', splits and take the 2nd part and add to the previous line:

Code:

              
if line.startswith('+'):
   line2 = line.split('+',1) [1]
   outfile.write(repr(prev)+(line2))
   print repr(prev)+(line2),"\n"
   if not line.startswith('m')|line.startswith('+'):
       outfile.write(line),"\n"

   prev = line    
input:

.subckt inv in out vss vdd

mp1 out in vdd vdd pfet w=80e-9 l=30e-9 nf=1 ps=312e-9 pd=312e-9 as=6.08e-15 $ anything after a dollar sign$ on a line is a comment
+ad=6.08e-15 sa=76e-9 sb=76e-9 sd=96e-9 ptwell=0 plorient=1 pccrit=1 p_la=0 ngcon=1

output I get:

'mp1 out in vdd vdd pfet w=80e-9 l=30e-9 nf=1 ps=312e-9 pd=312e-9 as=6.08e-15 'ad=6.08e-15 sa=76e-9 sb=76e-9 sd=96e-9 ptwell=0 plorient=1 pccrit=1 p_la=0 ngcon=1

Question:

why I am getting the 1st line in ' ' ex: 'mp1 --- as'

PS. script removed things after $ - as coded.

Thanks for your help.

Uttam
Reply
#2
(Jul-10-2017, 06:39 PM)uttamsaha2 Wrote: why I am getting the 1st line in ' ' ex: 'mp1 --- as'

Because that's what repr() does with strings, it put them in quotes. You should just be able to add it to the other string straight up without repr. If prev might not be a string, you would want to use str() instead.

Also, outfile.write(line), '\n' will not add the return character to outfile. It will create a tuple of the the output of write and the return character, which will then be ignored. I think you want outfile.write(line + '\n').
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you very much for your response. It helped, however, I got another issue:

code:
          if not line.startswith('*')|line.startswith('$'): # skips commented lines   << (3)
              if '$' in line:
                 line = line.split('$', 1) [0]     << (1)

              if line.startswith('+'):
                 line2 = line.split('+',1) [1]  << (2)
                 outfile.write(prev+str(line2) + '\n')
              if not line.startswith('m')|line.startswith('+'):
                 outfile.write(line + '\n')
              prev = str(line)    
What I try to do:
(1) if $ split & take [1] -- list
(2) if starts with + take [0] -- list
(3) if none - then the line is string

Here are the inputs:

mp1 out in vdd vdd pfet w=80e-9 l=30e-9 nf=1 ps=312e-9 pd=312e-9 as=6.08e-15 $ anything after a dollar sign$ on a line is a comment
+ad=6.08e-15 sa=76e-9 sb=76e-9 sd=96e-9 ptwell=0 plorient=1 pccrit=1 p_la=0 ngcon=1

mn1 out in vss vss nfet w=80e-9 l=30e-9 nf=1 ps=312e-9 pd=312e-9 as=6.08e-15
+ad=6.08e-15 sa=76e-9 sb=76e-9 sd=96e-9 ptwell=0 plorient=1 pccrit=1 p_la=0 ngcon=1

.ends $ For end of subckt

output:

mp1 out in vdd vdd pfet w=80e-9 l=30e-9 nf=1 ps=312e-9 pd=312e-9 as=6.08e-15 ad=6.08e-15 sa=76e-9 sb=76e-9 sd=96e-9 ptwell=0 plorient=1 pccrit=1 p_la=0 ngcon=1

mn1 out in vss vss nfet w=80e-9 l=30e-9 nf=1 ps=312e-9 pd=312e-9 as=6.08e-15 <<*
ad=6.08e-15 sa=76e-9 sb=76e-9 sd=96e-9 ptwell=0 plorient=1 pccrit=1 p_la=0 ngcon=1 <<*

.ends

I am getting the 1st mp1 together not the 2nd line mn1.

Appreciate any help.

Uttam
Reply
#4
Please use python tags. It's impossible to know what your code is doing without them. Indentation matters in python. See the BBCode tutorial link in my signature for instructions on how to use them.

How are you getting the lines? From looping over the file? If that's the case, then they end with a return character. The first m line gets it taken off when you split out the comment, but the second one doesn't have a comment. Use the strip method on your lines before you output them.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 1,266 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  spliting similar but multiple lines anna 7 4,870 Apr-20-2019, 08:53 AM
Last Post: anna

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020