cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to read data from txt file in python ?

Kaniz
Community Manager
Community Manager
 
2 REPLIES 2

dazfuller
Contributor III

If you want to read line-by-line in python then

with open('/path/to/file', 'r') as f:
    for line in f:
        print(line)

If you want to read the entire file to a list of lines

with open('/path/to/file', 'r') as f:
    data = f.readlines()

Or if you want to use pathlib

from pathlib import Path
 
p = Path('/path/to/file')
with p.open('r') as f:
    for line in p:
        print(line)
 
# or
 
with p.open('r') as f:
    data = f.readlines()

And then there's PySpark, Pandas, etc...

Kaniz
Community Manager
Community Manager

Thank you @Darren Fullerโ€‹ .

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.