How to Read from a text .txt file in Python! Pulling in data and filtering and modifying the info!

preview_player
Показать описание
Check out my personal channel for fun engineering content!
@peterlemaster8310

This video discusses the method for reading data into python from a text file. This includes step by step instructions for accessing the file from the same folder as the python program, then storing the data in a list, then filtering the special end line characters out and storing them in a modified list. There are numerous ways of filtering, processing and modifying the data read in and then you can perform any actions you could do with string variables with the data inside your python function once you learn how to read the data in!

Learn to write to text files here:

Love the content on this channel? Feel free to support me on Patreon!

If you have any questions or comments or want to see something specific in the future let us know about it with a comment!

We have hundreds of hours of instructional content on the channel and create more every week so be sure to subscribe to stay up to speed! Thanks for checking out the video and good luck with your projects!

Рекомендации по теме
Комментарии
Автор

Thank you. This was very easy to follow as a beginner.

iotekhaneokwire
Автор

Can you store a series of lists in a txt file to be able to use each list as the source for a Tkinter combobox?
I have tried a json file but that doesn’t allow the append method.

Paul-lypw
Автор

@LeMaster Tech Hey I was wondering if I can use this to match text and account for it? Do you have a video of that, or should I use something totally different?

jeff
Автор

What if they are numbers? I've got \t at the end of all of the numbers and this doesn't get rid of it

AdamWrigley
Автор

Thank you for this useful video,
I have a text file with headers before each column, how to skip those headers?
Should be grateful if you point to some of your videos if you explain this case.

hananali
Автор

Lets say i want to capture data for "12345", how to do that?

nurzulhafizyacob
Автор

hello sir, while i'm trying to remove the blackslash, i'm getting this(', ' ', ' ', ) at the end of the first line. How can I remove this?

balachandrasaimc
Автор

Very useful, how would you extract specific data from the text. I have a text with alpha and numerical data. I want to read just the numbers, then sort, find the length and the median. I see many videos on how to read the file and work with it, but not how to extract specified data only.

genevievehigdon
Автор

brother you said there is a way to from where ever the directory txt file, a file path joint please help me with that one i dont know how to do that please brother

zwephyaewin
Автор

Hey, I am struggling with a large and long tabular .txt file, about 400 MB which basically is a wind dataset from the automatic weather station. I need to select the days where the wind speed exceeds 90 km/h. My file is sooo long that it contains data from 1998 to 2021. How to do that in python?

greeshmasurendran
Автор

for line in read:
line = line.strip() #I think this line is missing
if line not in modified:
modified.append(line)

issamhammadi
Автор

for line in read:
if line not in modified:
modified.append(line.strip())

hi, this line of code doesn't work with removing the duplicate data in my file.

allaboutsophia
Автор

Am I missing something - on my computer this code does not work for stripping duplicates. On my computer I have to use IF LINE [:-1] NOT IN MODIFIED: to strip duplicates because it is otherwise searching for the line+\n (and thus doesn't avoid duplicates.)

willishuang
Автор

Thanks for the video. It is very helpful for beginners. I would like to know how to increase the reading and writing speed if I have 10000 lines in my text file

setproject-szku
Автор

What software are you using for the editor, is that Visual Studio Code?

Yobachi
Автор

can i type the modified variable with pyautogui ?

AboodyHandal
Автор

Hello, I've been hitting my head against the wall (i've tried str, json, pandas and ntf) for a while and I can't seem to arrive at my destination, hopefully I can get some help?

I get a result from a Cisco command and i want to read (focus) four lines of the output and then do something if something is present.

The output is this:
```
TDR test last run on: June 01 07:50:18

Interface Speed Local pair Pair length Remote pair Pair status

Gi1/0/11 auto Pair A 0 +/- 5 meters N/A Open
Pair B 0 +/- 5 meters N/A Open
Pair C 0 +/- 5 meters N/A Open
Pair D 0 +/- 5 meters N/A Open
```

The idea is, if I get to see Open or Short on the last column, then i want it otherwise ignore and go on to the next etc.
It is that one particular lines 5, 6, 7 and 8 that I want to examine and do something if I find what I am looking for.

Thoughts?





import pandas as pd

pd.set_option("display.max_colwidth", None)

with open("data.txt") as f:
dataline = f.readlines()
#print(dataline)
df = pd.DataFrame(dataline)
#print(df)
#print(df.loc[4])

Open = "Open"
Short = "Short"

if not df.empty:
#print(df)
if Open in str(df):
print(df)

elif Short in str(df):
print(df)

exlrin
Автор

It might be a REAL GOOD IDEA to post the code

augiblutz
Автор

Hey it's not working. File not found error:Error 2.please help.

AnkitG
Автор

You didn't close your file !!!
with open("file.txt", "r") as f:

samarbid
welcome to shbcf.ru