0

I have a file (list.txt) with huge array like this:

['1458575', '1458576', '1458577', '1458578'...]

I want to read it in my script but the output is white. The goal is print the number of line (list.txt) of the file.txt

numbers_line = open("list.txt", "r")
lines = numbers_line.readlines().split(',')
i=0
f=open('file.txt')
for line in f:
    if i in lines:
        print (line)
    i+=1

However, if I put the array direct it's read, but considering that is a huge array this is not could be helpful.

lines=['1458575', '1458576', '1458577', '1458578', '1458579', '1458580', '1458581', '1458582', '1458583', '1458584']
i=0
f=open('file.txt')
for line in f:
    if i in lines:
        print (line)
    i+=1

Thanks for your support

Hdez
  • 151
  • 7
  • do you want to read array/list as a line or the the numbers one by one? – Anurag Dhadse Aug 22 '20 at 13:52
  • `i` is of type `int` and `lines` contains `str`s... they will never equate... – RichieV Aug 22 '20 at 13:53
  • [This answer](https://stackoverflow.com/q/15197673/6692898) shows how to convert a string containing a list to an actual list with `ast.literal_eval` – RichieV Aug 22 '20 at 13:59
  • @AnuragDhadse I want to read number by number present in the array (list.txt); this is the number of line that I want to print in other file(file.txt). – Hdez Aug 22 '20 at 14:05
  • @RichieV thanks, but the list is integer numbers, maybe the variable name was wrong and confuse. – Hdez Aug 22 '20 at 14:06
  • you are showing a list of strings `['1458575', '1458576', '1458577',...`, notice how the accepted answer needs to cast to a list of `int`s – RichieV Aug 22 '20 at 14:36
  • Oh I see. Yes, you have right. Thanks for your support. – Hdez Aug 22 '20 at 15:12

3 Answers3

0

This can work

with open('file.txt') as file:
    content = file.read()
a = []
exec('a = ' + content)  # executes - a = ['1458575', ...]

Alternate method - copy file.txt to new file my_lists.py. Add 'a = ' at the start of my_lists.py file and execute

from my_lists import a
0

Use with open() as : instead of open() and close(), because it closes the connection to file automatically when errors occurs while the traditional manual open() and close() don't do.

lines=['1458575', '1458576', '1458577', '1458578', '1458579', '1458580', '1458581', '1458582', '1458583', '1458584']
lines = [int(x) for x in lines] # convert to integers

fpath = "list.txt"

with open(fpath, "r") as f:
    for x in f:
        x = x.trim() # remove end of line character
        if int(x) in lines:
            print(x) 
Gwang-Jin Kim
  • 9,303
  • 17
  • 30
0

readlines returns list of lines, not string

readline will return a string, but split(',') would give a list of string not int

try this

import ast
text_file = open("list.txt", "r")
lines = list(map(int, ast.literal_eval(text_file.read().strip())))
i=0
f=open('file.txt')
for line in f:
    if i in lines:
        print (line)
    i+=1
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sayan Dey
  • 771
  • 6
  • 13
  • thanks a lot, it doesn't work, the output file is white. What can I do about this? – Hdez Aug 22 '20 at 14:08
  • Sorry, my bad, I have edited my answer, you can try the current one, I chnaged ```list(map(int, ast.literal_eval(text_file.read().strip())))``` – Sayan Dey Aug 22 '20 at 14:11