Python (startswith) reg expression


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python (startswith) reg expression
# 1  
Old 05-15-2009
Python (startswith) reg expression

Hello together,

Yesterday I have recieved the script in the forum which works well.
This script should insert in the previous line, the line that starts with ";".
I'd like this process to recur after any arbitrary sign unless there's a number as a sign at the beginning of a line.


Actual state:

#!/usr/bin/env python


import sys


data=open(sys.argv[1]).read().split("\n")
for n,items in enumerate(data):
if items.startswith("A"):
data[n-1]= data[n-1]+items
data.pop(n)


print '\n'.join(data)





should be:

#!/usr/bin/env python


import sys


data=open(sys.argv[1]).read().split("\n")
for n,items in enumerate(data):
if not items.startswith('[0-9]'):
data[n-1]= data[n-1]+items
data.pop(n)


print '\n'.join(data)




Can I use regular expression at Phyton (startswith)?

I have carried out an investigation but unfortunately up till now I haven't found the right solution.

Any suggestion ??

Thanks in advance!
# 2  
Old 05-15-2009
Quote:
Originally Posted by research3
should be:

#!/usr/bin/env python


import sys


data=open(sys.argv[1]).read().split("\n")
for n,items in enumerate(data):
if not items.startswith('[0-9]'):
data[n-1]= data[n-1]+items
data.pop(n)


print '\n'.join(data)




Can I use regular expression at Phyton (startswith)?

I have carried out an investigation but unfortunately up till now I haven't found the right solution.

Any suggestion ??

Thanks in advance!
put your Python code in code tags. Python indentation is important so if you don't put in code tags, its hard to troubleshoot. of course you can use regular expression, using the re module. (i will leave it to you to read the docs if you are interested).
but for this case (and most cases), regular expression is not needed. you just take the first character of the line and check for isdigit(). eg
Code:
import sys
data=open(sys.argv[1]).read().split("\n")
for n,items in enumerate(data):
    if items[0].isdigit(): # check first character is a digit, ie equivalent to [0-9]
        data[n-1]= data[n-1]+items
        data.pop(n)

in a Python string, first character is position 0, second character position 1 and so on. so if you want to get third character of string, its string[2]. read the Python docs and study how Python works.
# 3  
Old 05-15-2009
Thanks again for your fast reply I' going look through the docs!

Something is wrong with the code or with my csv file, I will try to find out what the issue is, and I'll post my result.
# 4  
Old 05-15-2009
Just to clarify what ghostdog said...this "startswith" business is whats known as a "string method", and in string methods (there are many), it uses strings, not regular expressions.

The only way in python to deal with regex's is the "re" module

Code:
import re

the "re" module is pretty involved, but nifty, and in most cases it is overkill.

you know what always helps me in my python coding...that little $10 pocket reference book. I cant tell you how many times I found exactly what I needed in that thing, instead of sifting through docs for 30 minutes. Best $10 I ever spent.
# 5  
Old 05-15-2009
Quote:
Originally Posted by Gee-Money
instead of sifting through docs for 30 minutes. Best $10 I ever spent.
google with keywords takes less than 30 secs.. and free Smilie
# 6  
Old 05-15-2009
Quote:
google with keywords takes less than 30 secs.. and free
That's right!

Unfortunately, but the logic of the script is changed.


cat text

001;test;test;test
123;test;test;test
test;test;test
000;;test;test;test


./python.py text

result

123;test;test;test
test;test;test000;;test;test;test
001;test;test;test

Any idea?
# 7  
Old 05-15-2009
dude, so what should the output be like?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

REG Expression

Need your help in creating regular expression for particular set. let say I have given two dates 20130623 to 20140625. I need to create regular for the dates which fall in between above two dates. (4 Replies)
Discussion started by: gvkumar25
4 Replies

2. Shell Programming and Scripting

Confusion with Reg expression

I want to make a REG Expression to validate the directory. my dirsample is below: /abc/abc/abc abc/abc/abc abc/abc/abc/ /abc/a bc/abc /a bc/abc/abc /abc/abc/a bc / abc/abc/abc /abc/ abc/abc /abc/.abc /.abc/abc / // /abc /.abc And my code is below: grep -E '^\/(+\/?)+$' dirsample (4 Replies)
Discussion started by: franksunnn
4 Replies

3. Shell Programming and Scripting

reg expression in perl

how to uniquely match each of the words seperated by / in perl ${REP_PATH}/FUNCTIONAL/wide1c_1.0V/max/qor.rpt https://www.unix.com/images/misc/progress.gif (5 Replies)
Discussion started by: dll_fpga
5 Replies

4. Shell Programming and Scripting

reg expression in perl

./GEN_SCR.pl -f ${REP_PATH}/FUNCTIONAL/wide1c_1.0V/max/qor.rpt -o ${REP_PATH}/FUNCTIONAL/GEN2_wide1c_1.0V_max.csv where GEN_SCR.pl is as below...i need to check whether max or min is coming in the argument to the script ...how to do this? ${REP_PATH}/FUNCTIONAL/wide1c_1.0V/max/qor.rpt ... (0 Replies)
Discussion started by: dll_fpga
0 Replies

5. Shell Programming and Scripting

Reg expression

Please let me understand this reg expression (\s+')(.*)('\s+)(.)(.*)(\/.*)/) i have doubt in the below 2.I'm not understanding why back-tick used? (\s+') and ('\s+) (2 Replies)
Discussion started by: dll_fpga
2 Replies

6. Shell Programming and Scripting

awk reg expression

Hello, I have thousand of messages (HL7), I want to use awk to extract only the ones that have a particular value in pv1.18 Each record in the file is the whole HL7 message, ie. when I print $0 I get the whole message MSH EVN PID etc. ,there is an x0d between the segments. I would like to use a... (3 Replies)
Discussion started by: gio001
3 Replies

7. Shell Programming and Scripting

Reg expression For

HI system.sysUpTime.0 : Timeticks: (1519411311) 175 days, 20:35:13.11 From the above output i need only 175days in a perl script.. Please Help (2 Replies)
Discussion started by: Harikrishna
2 Replies

8. Shell Programming and Scripting

perl reg expression

I have regular expression like this ( replace + with \+) ($mod_server) = ($server =~ /\+/\\+/g); the above is failing with error . what's wrong with it . Thanks (1 Reply)
Discussion started by: talashil
1 Replies

9. UNIX for Dummies Questions & Answers

Help with Reg. Expression

I need help with this: Can any one tell me what does these below mean: 1. "\(.\).*") != '/' 2. sed 's+^\./++;s+/.*++' 3. sed "s+${f}/+ + Thanks in advance (7 Replies)
Discussion started by: moe2266
7 Replies

10. Shell Programming and Scripting

Awk - Using a Shell Variable in the Reg Expression

Hi all, I have a shell variable $test1 that holds a value derived from some other processing. What I need to do is use that $test1 as the input to a awk regular expression: nawk -F"," -v tester=$test1 ' /tester/{ print $0 } ' $inputFile So what I have is tester... (6 Replies)
Discussion started by: not4google
6 Replies
Login or Register to Ask a Question