If you don’t want to use the
pandas library, you can convert a text file to a DataFrame in Python using the built-in
csv module along with the standard
dict data structures. Here’s an example:
# Open the text file in read mode
with open('input.txt', 'r') as file:
# Create a CSV reader
reader = csv.reader(file, delimiter='\t')
# Read the file contents into a list of rows
rows = list(reader)
# Get the header row
header = rows
# Create a list of dictionaries for each row
data = [dict(zip(header, row)) for row in rows[1:]]
# Print the data as a DataFrame-like representation
for row in data:
In this example, we open the text file
'input.txt' in read mode using
open() and create a CSV reader with
csv.reader(). We specify the delimiter as
\t to match the delimiter used in your text file.
Next, we use a list comprehension to read the file contents into a list of rows. Each row is a list of values.
We then extract the header row from the list of rows and store it in the
Finally, we create a list of dictionaries
data where each dictionary corresponds to a row in the text file. We use
zip() to pair the header values with the corresponding row values and
dict() to create a dictionary from each pair. We iterate over the rows starting from index 1 to skip the header row.
You can then work with the
data list of dictionaries, which represents the data in a tabular format similar to a DataFrame. In the example, we simply print each row for demonstration purposes.
This approach allows you to convert a text file to a DataFrame-like representation without relying on the