Reading Files with Multiple Lines in Python

  Рет қаралды 27,370

Adam Gaweda

Adam Gaweda

Күн бұрын

Пікірлер: 19
@smbunation
@smbunation Жыл бұрын
This is so perfect- a real-world example and straight to the point- and still relevant three years later- cheers Adam-
@kakairecharles3474
@kakairecharles3474 2 жыл бұрын
Million thanks. All is very simplified in an intersting way.
@sharmaaks729
@sharmaaks729 3 жыл бұрын
Exactly what I was searching for... thanks a lot
@hungtruong5523
@hungtruong5523 Жыл бұрын
Oh god, thank you, my savior
@jaggyjut
@jaggyjut 3 жыл бұрын
Great explanation. Would you be able to create a tutorial on reading values in a text file using splitlines, previous_lines nested in a for loop. Basically like to know how to take field & respective value from a text file and output them into a pandas dataframe.
@AMGaweda
@AMGaweda 3 жыл бұрын
I'm not quite sure what you mean, but I HAVE had to create a blank DataFrame from scratch before. Start by creating a blank DataFrame with a list of field names. new_df = pd.DataFrame(columns=['fname', 'lname', 'email', 'password']) # for example Then, as you iterate through your text / data, create a blank dictionary and then add in the values to the dictionary entry = {"fname": "Adam", "lname": "Gaweda", "email": "abcdefg@email", "password": "bubbles"} Finally, append this entry to the DataFrame and then replace the old DataFrame with the newly appended version. new_df = new_df.append(entry, ignore_index=True) The ignore_index parameter says "Don't worry about where to add the entry, just put it at the bottom".
@Kig_Ama
@Kig_Ama 4 жыл бұрын
How can I read from file faster via multiprocessing using Python?
@willfenno2094
@willfenno2094 3 жыл бұрын
how would you find the maximum temperature from what you outputted at the end?
@AMGaweda
@AMGaweda 3 жыл бұрын
In that case you'd want to create a "current_max" variable before the loop, it could be set to -1 or -10000 (mostly depends on your data). Then, as you traverse the lines in the file, you'd do a conditional statement to see if reading > current_max. If so, update current_max to be this new largest value.
@mouradhamdaoui1393
@mouradhamdaoui1393 2 жыл бұрын
pls how can i replace a values with NAN
@cliosport1374
@cliosport1374 7 ай бұрын
👍👌👍
@НеъматовИсломбек
@НеъматовИсломбек 3 жыл бұрын
thanks that was really helpful !!!
@ronnievincent7895
@ronnievincent7895 4 жыл бұрын
If i have a list with strings having 2 values and 1 value for example Adair, KY Jackson, AL Chicago,IL ALASKA how could i rewrite my data to a new file switching state and city and leaving out the uppercase state for example KY: Adair AL: Jackson IL:Chicago When I run what i got i get a 'list index out of range error' because of the uppercase State only having on value. This is the code I have so far. I've been searching for help for 2 days, so anything you got would be much appreciated. county_file = open("c:\\Python 3.8.3\\us-counties.2.txt", "r") lines = county_file.readlines() state_file = open("c:\\Python 3.8.3\\Ronnie.Vincent.County.Seats.manipulated.txt", "w") for aline in lines: values = aline.split() print(values[1],':', values[0]) state_file.close() county_file.close()
@AMGaweda
@AMGaweda 4 жыл бұрын
In your line "print(values[1],':', values[0])", you are assuming there is a values[1]. You could check the length of values via "if len(values) == 2" before printing to know whether the line wa split into a list with multiple elements. If that conditional is false, say like with Alaska, you could handle it in with an 'else'.
@chandrakanthurs7470
@chandrakanthurs7470 3 жыл бұрын
What can we do if data in table form is not alligned , how can we extract that index
@AMGaweda
@AMGaweda 3 жыл бұрын
If your data is separated by something like a comma (,) then your file is using what is known as a delimiter to separate values. You can handle this a few different ways: 1) The line.split function can include '"," inside it which will split on the commas [so, line.split(",") ]. 2) If you are using a CSV file, you can also use Python's csv library to process the file for you. You can see how this works in this video - kzbin.info/www/bejne/rHqwZKKfhJ2qatE
@cherifadala
@cherifadala 2 жыл бұрын
Thanks
Writing to Files in Python
10:15
Adam Gaweda
Рет қаралды 1,6 М.
Python Tutorial: File Objects - Reading and Writing to Files
24:33
Corey Schafer
Рет қаралды 1,8 МЛН
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 21 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 2,5 МЛН
Creative Justice at the Checkout: Bananas and Eggs Showdown #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 34 МЛН
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 135 МЛН
This Is Why Python Data Classes Are Awesome
22:19
ArjanCodes
Рет қаралды 817 М.
Read A Specific Line From A File | Python Example
9:15
Portfolio Courses
Рет қаралды 22 М.
Read Giant Datasets Fast - 3 Tips For Better Data Science Skills
15:17
Python Simplified
Рет қаралды 53 М.
#5 Read and process multiple text files in Python
9:38
Data Skills for Everyone
Рет қаралды 24 М.
How To Use Dunder Methods In Python Tutorial (Magic Methods)
6:36
#9 Basic Python Data Extraction from Text Files
20:13
Data Skills for Everyone
Рет қаралды 10 М.
#65 Python Tutorial for Beginners | File handling
12:14
Telusko
Рет қаралды 719 М.
Using lists to find and calculate values in a text file
10:27
TheEduGeek
Рет қаралды 7 М.
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 21 МЛН