Python Forum
Reading data to python: turn into list or dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading data to python: turn into list or dataframe
#1
Hi,

I tried 3 different ways to read csv data into python.

The 1st and 2nd methods turn data to dataframe and list, and they both work OK

The 3rd method, return something with:

type(dt): <class '_io.TextIOWrapper'>

print(dt): <_io.TextIOWrapper name='C:\\temp\\LongPctls.csv' mode='r' encoding='cp1252'>

Can you please help explain what form of data is that?

When I use for loop, I can still print out dt.

Thank you, Hong
#Method 1: return dataframe
    file_name_csv="C:\\temp\LongPctls.csv"
    df=pd.read_csv(file_name_csv)
    print(type(df))
    print(df)

 #Method 2: return list
    file = "C:\\temp\LongPctls.csv"
    data = open(file, "r")
    list = data.read().split('\n')
    print(list)
    print(type(list))
    
# Method 3: ?????
    with open("C:\\temp\LongPctls.csv",  mode="r") as dt:
            print(type(dt))
            print(dt)
            for i in dt:
                print(i)
Reply
#2
This is fileobject
hhchenfx likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
You can also use:
with open("C:\\temp\LongPctls.csv") as fp:
    crdr = csv.reader(fp) # can add optional [inline]delimiter='.'[/inline] if not comma (replace . with delimiter)
    for row in crdr:
        print(f"type row: {type(row)}, row: {row}"
Using this method , if there is a header, it will be the first row

Or, if you prefer a dictionary, use:
with open("C:\\temp\LongPctls.csv") as fp:
    crdr = csv.DictReader(fp) # can add optional [inline]delimiter='.'[/inline] if not comma (replace . with delimiter)
    for row in crdr:
        print(f"type row: {type(row)}, row: {row}"
Each row will be a dictionary.
hhchenfx likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from mysql. stsxbel 2 390 May-23-2021, 06:56 PM
Last Post: stsxbel
  How to save json data in a dataframe shantanu97 1 437 Apr-15-2021, 02:44 PM
Last Post: klllmmm
  Reading and appending list MrSwiss 1 366 Mar-01-2021, 09:01 AM
Last Post: Serafim
  reading canbus data as hex korenron 9 1,123 Dec-30-2020, 01:52 PM
Last Post: korenron
  Pandas Extract data from two dataframe nio74maz 1 471 Dec-26-2020, 09:52 PM
Last Post: nio74maz
  Reading Serial data Moris526 6 903 Dec-26-2020, 04:04 PM
Last Post: Moris526
  wrong data reading on uart fahri 6 849 Sep-29-2020, 03:07 PM
Last Post: Larz60+
  convert list to five columns dataframe in sequence tonycat 2 722 Sep-29-2020, 06:47 AM
Last Post: tonycat
  How to map dataframe to list Mekala 1 498 Aug-09-2020, 12:07 AM
Last Post: scidam
  Import CSV data into array and turn into integers DoctorSmiles 5 944 Jul-16-2020, 10:47 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020