0

i need to get string by using Line-number as i have two files . the first is containing the line numbers and the second containing the text it self like this

File1 is for line number

   5
   4
   1 
   7
   2
   3

File2 is for the text i need it

-138.84 148.007
-155.57 141.28
-138.173    147.832
-135.094    134.222
-139.843    137.315
-138.619    149.688
-149.256    146.325
-125.212    145.902
-116.355    141.826

the result should be a file containing

-139.843    137.315
-135.094    134.222
-138.84 148.007
-149.256    146.325
-155.57 141.28
-138.173    147.832

how can i start please as i searched a lot but found method that got line number for string not vica versa

i tried this code

fp = open("file1","r")

listt= [] 
lines = []
for line in fp.readlines():
    listt.append(line)
for i in listt:
    print(i)
    x = linecache.getline('file2.txt', i)
    print(x)    
    lines.append(x)
#print(lines)

but got

    Traceback (most recent call last):
  File "get-text.py", line 12, in <module>
    x = linecache.getline('file2.txt', i)
  File "/usr/lib/python3.6/linecache.py", line 17, in getline
    if 1 <= lineno <= len(lines):
TypeError: '<=' not supported between instances of 'int' and 'str'
user
  • 87
  • 9
  • 1
    where is linecache in your code ? – Deepak Tripathi May 02 '22 at 20:29
  • What line returns that error? You do not appear to have a `<=` comparison. – jordanm May 02 '22 at 20:29
  • @DeepakTripathi it is a library i imported it `import linecache` – user May 02 '22 at 20:30
  • @jordanm yes that's right . i post the full error i got – user May 02 '22 at 20:31
  • so use linecache.getline('file2.txt', int(i)) and let me know if it works – Deepak Tripathi May 02 '22 at 20:31
  • Think carefully about what happens when you read the file and create the list `listt`. Does it contain integers? No, it contains strings, because that's what you get from `fp.readlines`. If this is enough for you to understand the problem, then it is a simple oversight, and you [should](https://meta.stackoverflow.com/questions/261592/) study [how to solve simple problems yourself first](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) before asking on Stack Overflow. Otherwise, please see the duplicate I have linked. – Karl Knechtel May 02 '22 at 20:32
  • @DeepakTripathi ohh i'm in this error for hours and didn't take attention to parsing !! Thanks a lot you saved my time – user May 02 '22 at 20:33
  • @KarlKnechtel . i tried already but didn't take my attention to the type of the list as i thought it is a number and I've already printed it .. thanks for your time – user May 02 '22 at 20:35
  • "but didn't take my attention to the type of the list as i thought it is a number" This is why, when you get an error message, the first step should be to *read* and *try to understand* the error message. It says right there that there is a `TypeError`, i.e., an error caused *by the type* of something. It shows you the code: `if 1 <= lineno <= len(lines):`; clearly the thing that is the wrong type is either `1`, or `lineno`, or `lines`, or the `len(lines)` result. – Karl Knechtel May 02 '22 at 20:41
  • After thinking about what the code is intended to do (do a numeric comparison), clearly the `len` call is correct (it gives an integer), and the `1` is correct (it is an integer). It is straightforward to see that passing `lines` to `len` is correct, since we know that `lines` is a list, and we want its length. By process of elimination, only `lineno` remains. "and I've already printed it " Well, yes; `print` makes integers and digit-strings look the same. It isn't showing you the quote-marks on a string; that's its purpose (after all, you don't always want to see them). – Karl Knechtel May 02 '22 at 20:42

0 Answers0