Python 3, Read textfile and separate each line and fill in corresponding variables
Here's something that should work:
first we need to get all the lines read into a list.
>>> with open('in.txt', 'r') as f:
>>> lines = f.read().splitlines()
now we can index nicely in:
>>> print(lines[0])
John|Graham|JOB|AGE
Okay, now we want to split each line into the data we need.
>>> first = lines[0]
>>> info = first.split('|')
['John', 'Graham', 'JOB', 'AGE']
And then to put everything into a variable:
>>> NAME, LASTNAME, JOB, AGE = info
>>> print(NAME)
John
>>> print(LASTNAME)
Graham
Putting it all together:
with open('in.txt', 'r') as f:
lines = f.read().splitlines()
def get_info(lines, line_num):
line = lines[line_num]
return line.split('|')
NAME, LASTNAME, JOB, AGE = get_info(lines, 0)
print(NAME) # Prints "John"
If you don't want to read in the whole file at once, you can use the same techniques in your script:
with open(filename) as fp:
for i, line in enumerate(fp):
print(i)
print(line)
if i == index:
NAME, LASTNAME, JOB, AGE = line.split('|')
break
How can I attribute each line of a txt file to a variable?
Instead of having so many variables, a better solution would be to use a dictionary instead. So basically something like:
my_dictionary = {}
with open('sentences.txt', 'r') as file:
lines = file.readlines()
for line in lines:
my_dictionary[lines.index(line)] = line.strip()
print(my_dictionary)
So to access the line, you can just access the key of that dictionary, making your code efficient and clean :)
Making specific columns in a text file into a list
You could use pandas
, it also fixes the scientific notation:
import pandas as pd
df = pd.read_csv('filename.txt', delim_whitespace=True, header=None, skiprows=3, skipfooter=4, engine='python')
Output df
:
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | 0.0 | 0.0 | 6371000.0 | 0.4843809 | 2001.0 |
1 | 0.0 | 0.0 | 6371000.0 | 0.4635282 | 2022.5 |
2 | 0.0 | 0.0 | 6371000.0 | 1.1587459999999998 | 1984.5 |
3 | 0.0 | 0.0 | 6371000.0 | 1.15313 | 1960.5 |
4 | 0.0 | 0.0 | 6371000.0 | 0.48739209999999994 | 1986.5 |
how to read multiple input data listed on the same line of a file in python
you can just use below code:
f = open('1.txt' , 'r')
lines = f.read().split(" ")
so you have list of values and you can take it in variable if you want like below:a = lines[0]
, b = lines[1]
and so on.
You can use a1,a2,a3 = f.read().split(" ")
if you want it in variables. Provided you have exact number of variables as values in file.
Hope this helps.
Related Topics
How to Get Max Output from a While Loop
Saving the Output of a Python Program
Printing the Number of Days in a Given Month and Year [Python]
Easiest Way to Ignore Blank Lines When Reading a File in Python
How to Start a Background Process in Python
Passing a List of Values from Python to the in Clause of an SQL Query
How to Make Python Code to Execute Only Once
How to Continue a Loop After Catching Exception in Try ... Except
Pandas: Subtracting Two Date Columns and the Result Being an Integer
Parentheses and Quotation Marks in Output
How to Save All the Variables in the Current Python Session
How to Calculate Average a Dictionary from List of Dictionary Data
Use Variable as Key Name in Python Dictionary
How to Check If Numbers Are in a List in Python
Keras Valueerror: Input 0 Is Incompatible With Layer Conv2D_1: Expected Ndim=4, Found Ndim=5