I'm trying to do some simple parsing
as example:

import csv
import os,sys
spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ',quoting=csv.QUOTE_MINIMAL)
string = "cat /home/myfile.txt |grep  -m 1 "my game" "
string2=os.system(string)
spamWriter.writerow([string2])

home]# python excel2.py
File "excel2.py", line 4
string = "cat /home/myfile.txt |grep -m 1 "my game" "

^
SyntaxError: invalid syntax

home]# cat /home/myfile.txt |grep -m 1 "my game"
my game is football

What am i doing wrong here?

^
SyntaxError: invalid syntax
is pointing to "my game"

Use 'quotes as outer quotes or double the " inside quotes.

solved this by adding a \ :)

However now run into this:

import csv
import subprocess
import os,sys
spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ',quoting=csv.QUOTE_MINIMAL)
#string = "cat /home/myfile.txt |grep  -m 1 \"my game\" "
#string2=os.system(string)
#spamWriter.writerow([string2])

proc = subprocess.Popen(["cat /home/myfile.txt |grep  -m 1 \"my game\" "], stdout=subprocess.PIPE, shell=True)
#out = proc.communicate()
(out, err) = proc.communicate()
print "program output:", out
print out
spamWriter.writerow([out])

I get :

# python excel2.py
program output: my game is football

my game is football

-note the space... How do i get rid of that?

and csv file reads:
# cat eggs.csv
"my game is football
"
how to remove the quotes there and fix this?

Use 'quotes as outer quotes or double the " inside quotes.

What space?

str_out = 'cat /home/myfile.txt |grep  -m 1 "my game" '
##                                                   ^<-- this space???
proc = subprocess.Popen([str_out], stdout=subprocess.PIPE, shell=True) 
#
# in Python
found = False
for rec in open("/home/myfile.txt", "r"):
    if (not found) and ("my game" in rec.lower()):
        print rec.strip()
        found = True     ## first record only

Why do you need to do system call with command line grep?,you can easily write this without it.
Python has lot more power an dont need command line call like this to do this simple task.

Correct, the grep "this string" myfile.txt is equivalent of

print ''.join(line for line in open('myfile.txt') if "this string" in line)

(untested as I write from my Nokia E7)

-m 1 (--max-count=one) = find the first occurrence only, so something like:

found = [line.strip() for line in open('myfile.txt') if "this string" in line.lower()]
print found[0]
#
# or if you like debuging hell
print [line.strip() for line in open('test1') if "my game" in line.lower()][0]

Or

tosearch ='my text'
try:
    print(next(line for line in open('myfile.txt') if tosearch.lower() in line.lower()))
except StopIteration:
    print('Text %r not found in file' % tosearch)

If you prefer clean code ;)

Or

tosearch ='my text'
try:
    print(next(line for line in open('myfile.txt') if tosearch.lower() in line.lower()))
except StopIteration:
    print('Text %r not found in file' % tosearch)

If you prefer clean code ;)

tony did you try your code.???
Next come with a yield iter and calling next without any import or a method/class defined for next.... Do you recomemed that??

I think is not a good stuff. Also i wonder if this algo with produce a result.
from my htc phone.
:)

Sure I did. What is the problem? Open is a nice iterable. Here version which does this one line match grep for two command line arguments (without check of argument count, just basic stuff)

from __future__ import print_function
import sys
import glob
tosearch = sys.argv[1]
try:
    print('%s: %s' % next((filename, line) 
          for filename in glob.glob(sys.argv[2]) 
          for line in open(filename) if tosearch.lower() in line.lower()))
except StopIteration:
    print('Text %r not found in files.' % tosearch)

Only bad point I can find is that files which had not the file are not closed manually or with with, but only by Python's garbage collector when there is no reference to them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.