-4

I am looping through each row (line containing "fields" separated by spaces) inside a data file and would like to compare a substring of one field with another static value. If the comparison is true I would like to print a string 'X' otherwise 'Y'. Just wondering how can it be done using Python. Any help would be appreciated. Thanks.

Code :-

for i in inputm[1:]:    
    print('\n',i[0].split(':')[0]
              ,str(datetime.strptime(i[0].split(':')[1],'%Y%m%d'))[:10]
              ,i[1],round(sum( float(v) if v else 0.0 for v in i[2:6])/4,2)
              ,i[6][0:23]
          )

Input :-

1:20160101  123 10  20  0   0   http://www.google.com/favorites 
2:20170101  234 20  30  10  0   http://www.doodle.com/favorites

Output :-

1 2016-01-01 123 7.5 Correct
2 2017-01-01 234 17.5 InCorrect

Comments :- I am really interested in this piece of code.

  i[6][0:23]

Would like to compare the above substring with http://www.google.com and if they match then print Correct else InCorrect.

Teja
  • 13,214
  • 36
  • 93
  • 155
  • tuple inside a file? Substring of a column? What exactly are you talking about? These words have meanings, please stick to standard terminology. And what do you mean within a print statement? First, `print` is a function not a statement in Python 3, and I'm not sure why you want to do it inside anyway. – juanpa.arrivillaga Apr 04 '17 at 22:01
  • It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far (forming a [mcve]), example input (if there is any), the expected output, and the output you actually get (output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the [tour] and [ask]. – TigerhawkT3 Apr 04 '17 at 22:10
  • @TigerhawkT3 I am not demanding for code here... Being new to Python I explored and browsed on google but really didnt find anything to do conditional printing with in a print statement.... – Teja Apr 04 '17 at 22:15
  • The more expressions you cram into a single statement, the less comprehensible they all become. – Kevin J. Chase Apr 04 '17 at 22:35
  • You're trying to add something to code that's already present in it, so you apparently neither wrote nor read it. It looks very much like a code-writing request to me. – TigerhawkT3 Apr 04 '17 at 22:47

3 Answers3

-1

Python offers a ternary-style expression syntax that looks like this:

value1 if condition else value2

You can use that to print:

x = some_number()
print( 'X' if x < 10 else 'Y' )
aghast
  • 14,785
  • 3
  • 24
  • 56
  • 1
    the question already includes a conditional expression, so presumably, the author knows about them... – juanpa.arrivillaga Apr 04 '17 at 22:02
  • Nah. OP got that from another question about 30 mins ago. – aghast Apr 04 '17 at 22:03
  • Yes, it looks like OP picked up Python earlier today and is trying to produce code by getting SO to write it for him/her. That doesn't make this answer relevant (if it were, it'd be a duplicate of [this](http://stackoverflow.com/questions/11880430/how-to-write-inline-if-statement-for-print) anyway). – TigerhawkT3 Apr 04 '17 at 22:09
-1

Don't try to do everything in one go, divide and conquer:

# read the lines
with open('myfile.txt', 'rb') as fp:
    rows = fp.readlines()

# split the lines into fields
rows = [row.split() for row in rows]

# create a function to format field values
def fmt_row(row):
    res = []
    res += row[0].split(':')  # split the first field on :
    res += [float(field) for field in row[1:-1]]  # convert all but first/last field to float
    date = res[1]
    res[1] = '%s-%s-%s' % (date[:4], date[4:6], date[6:])
    return res

# convert/format all the rows
rows = [fmt_row(row) for row in rows]

# finally create the output
output = [[
    row[0],   # first output field
    row[1],   # second..
    round(sum(row[2:-1]/4), 2),
    'Correct' if row[-1] == 'http://www.google.com/favorites' else 'InCorrect'
] for row in rows]

# print the output?
print '\n'.join([' '.join(row) for row in output])
thebjorn
  • 26,297
  • 11
  • 96
  • 138
-2

You could use inline if statement -

"Correct" if some_condition else "InCorrect"

This will return "Correct" if condition is True.

But I really suggest you to use some intermediate variables. Your code is unreadable.