0

I want the data from the txt file to be in an array My txt file contains names written one after the other on a new line:

Sophia
Emily
Mary
Linda
Madison
Susan
Lily
...

I want them in the table

name = [ 'Sophia', 'Emily', 'Mary', 'Linda', 'Madison', 'Susan', 'Lily', ...]

it -> ... means that there is still n amount of data, eg it's thousands

i open my file with

with open('dane.txt') as x:
    contents = x.read()

I'm not sure if it's correct but it's reading the data so far

i wanted to use

import numpy as np
name=np.loadtxt("dane.txt",dtype="str")

but i don't know why i get "Import "numpy" could not be resolved"

Felipe
  • 1
  • 1

2 Answers2

0

first did you try to pip install numpy on your machine.

secondly you can do that natively in python with this function

def get_names(file):
    with open(file, 'r') as f:
        array = f.readlines()
    return array
Curtis
  • 1
  • 1
0

You don't need numpy for that. You can simply do:

with open('dane.txt') as x:
    contents = x.readlines()
Joan Lara
  • 1,362
  • 8
  • 15