-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.

5
  • 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. Commented Apr 4, 2017 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 minimal reproducible example), 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 How to Ask. Commented Apr 4, 2017 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.... Commented Apr 4, 2017 at 22:15
  • The more expressions you cram into a single statement, the less comprehensible they all become. Commented Apr 4, 2017 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. Commented Apr 4, 2017 at 22:47

3 Answers 3

-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' ) 
Sign up to request clarification or add additional context in comments.

3 Comments

the question already includes a conditional expression, so presumably, the author knows about them...
Nah. OP got that from another question about 30 mins ago.
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 anyway).
-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]) 

2 Comments

It doesn't work...
@Teja: "It doesn't work" is not a useful problem statement, and with over 150 questions to your name, you should know that.
-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.