-2

I'm saving a few integer values into a variable from a for loop and each value gets saved into a new line.

Ex: print(line)
1
2
3
4

How to count the number of values within the variable line (like it's 4 in this case). I tried with len(), count(), but it's not working.

Sample code:

fl = open('onlyfri.txt', 'r')
lines=fl.readlines()
lines = [x.strip() for x in lines]

for row in lines:        
    if row.find("-01-") != -1:
    print(str(lines.index(row)))
fl.close
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    Show your code to us – shaik moeed Aug 25 '23 at 04:12
  • 1
    How exactly do you put values into `line`? You're making it a string with embedded newlines? Then you need to count the number of newlines in the string. But it would probably make more sense to store the values in a list, where such operations can be much more easily handled. – deceze Aug 25 '23 at 04:14
  • See: [Find how many lines in string](https://stackoverflow.com/questions/34846413/find-how-many-lines-in-string) – Abdul Aziz Barkat Aug 25 '23 at 04:19
  • @AbdulAzizBarkat - Tried the solution which you provided. It ended up with below result. 1 1 1 1 – Siddarth kaundinya Aug 25 '23 at 05:12
  • @deceze - Basically I'm trying to get the line numbers from a text file and storing in a variable as below line = str(lines.index(row)) – Siddarth kaundinya Aug 25 '23 at 05:15
  • You'll need to present a more complete [mre]. It's unclear what exactly you're dealing with. – deceze Aug 25 '23 at 05:19
  • @deceze : Below is the sample code: fl = open('test.txt', 'r') lines=fl.readlines() lines = [x.strip() for x in lines] #print(lines) #print(type(lines)) for row in lines: if row.find("-01-") != -1: #fw.write('string exists in file') #fw.write('line Number:', lines.index(row)) print(str(lines.index(row))) fl.close – Siddarth kaundinya Aug 25 '23 at 05:29
  • [Edit] this into your question. – deceze Aug 25 '23 at 05:45
  • *"I’m saving few integer values into a variable"* — You're not doing any of that. You're just printing to the screen. If you were actually saving these values somewhere (for example *in a list*), then you could easily count them. Of course, you could also simply use another variable which you increment every time after you print something in the loop. Then after the loop that variable would indicate how often you printed. – deceze Aug 25 '23 at 06:30
  • @deceze - ah yes. Apologies. It was integer type before when i tried to check. Later, I included str in print command . Can you help with the updated code please? Thanks. Or, kindly let me know how can I print the result to a list and take the count (again if you can provide me the updated code for this scenario) – Siddarth kaundinya Aug 25 '23 at 06:37
  • Well, the entire thing can be reduced to `indices = [i for i, row in enumerate(lines) if '-01-' in row]`. Now you have a list of indices of rows in which "-01-" appears, and you can easily take the `len()` of that list too. – deceze Aug 25 '23 at 06:44
  • @deceze - Thank you very much for your help in this regard. – Siddarth kaundinya Aug 25 '23 at 09:03

0 Answers0