Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a .DAT file in python?

Tags:

python

I am making a basic program that has a quiz with multiple choice answers. I want to access the data from the .DAT file. Here's the basic layout of the .DAT file.

Which sport uses the term LOVE ?
Tennis
Golf
Football
Swimming
A

How can I access each line separately?

like image 425
Michael Haywood Avatar asked Aug 20 '13 16:08

Michael Haywood


People also ask

How do I read a .dat file in Python?

dat file to a CSV file in Python in four simple steps: (1) Install the Pandas library, (2) import the Pandas library, (3) read the CSV file as DataFrame, and (4) write the DataFrame to the file.

How do I read .dat files?

Most DAT files contain text, so you can open them with text editors, like Notepad, Notepad++, VS Code, and so on. If you are sure the information contained in the DAT file is a video or audio, then your media player can open it. If it's a PDF, then Adobe Reader can open it, and so on.

How do I read a column from a file in Python?

Python3. In this method we will import the csv library and open the file in reading mode, then we will use the DictReader() function to read the data of the CSV file. This function is like a regular reader, but it maps the information to a dictionary whose keys are given by the column names and all the values as keys.


1 Answers

for line in open(filename, 'r'):
    item = line.rstrip() # strip off newline and any other trailing whitespace
    ...

For the bonus: Tennis!

like image 148
mattexx Avatar answered Sep 22 '22 07:09

mattexx