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
  Help with to check an Input list data with a data read from an external source sacharyya 3 317 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Filter data into new dataframe as main dataframe is being populated cubangt 8 922 Oct-23-2023, 12:43 AM
Last Post: cubangt
  trouble reading string/module from excel as a list popular_dog 0 384 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,273 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Reading Data from JSON tpolim008 2 1,030 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  function returns dataframe as list harum 2 1,337 Aug-13-2022, 08:27 PM
Last Post: rob101
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,108 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  Need help formatting dataframe data before saving to CSV cubangt 16 5,511 Jul-01-2022, 12:54 PM
Last Post: cubangt
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,488 Jun-27-2022, 08:45 AM
Last Post: Tmagpy
  Using .append() with list vs dataframe Mark17 7 9,890 Jun-12-2022, 06:54 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

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