A
A
Anton Chernyshev2020-06-19 16:24:08
Python
Anton Chernyshev, 2020-06-19 16:24:08

How to import the value of a variable from a file?

tell me how i can pass value from txt file to variable

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
ankomserg, 2020-06-19
@ankomserg

import numpy as np

x = np.loadtxt(file_name, unpack = True)

you can add delimeters if 2 garbage separated by a space, for example
import numpy as np

x, y = np.loadtxt(file_name, unpack = True, delimiter = " ")

if there are many lines in the file then
import numpy as np

x, y = np.loadtxt(file_name, unpack = True, delimiter = " ")

x and y will be sheets with corresponding data
e.g. txt file "0 1\n1 2" will give 2 sheets x = [0,1] and y = [1,2]

M
Maxim, 2020-06-19
@XTerris

with open('file.txt', 'r') as file:
    a = file.readline().strip()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question