What is the best and easiest way to read the text file delimited by tab in python? I want to convert first column of text file into a list escaping first line [header].
import csv
with open ['data.txt', 'r'] as f:
first_row = [column[0] for column in csv.reader[f,delimiter='\t']]
print [first_row]
The code above gives all the elements of first_column. How can I escape first line [header]?
asked Jun 12, 2013 at 1:51
lisalisa
611 gold badge2 silver badges8 bronze badges
2
Maybe I'm missing something in the question, but why not just slice off the first element of the list?
import csv
with open ['data.txt', 'r'] as f:
first_column = [row[0] for row in csv.reader[f,delimiter='\t']]
print [first_column[1:]]
answered Jun 12, 2013 at 2:04
Dave CostaDave Costa
46.4k8 gold badges55 silver badges71 bronze badges
0
Once you load the file, you can access data by column name. In this example, FirstColName
is the first column name of the loaded file.
import pandas as pd
import numpy as np
file = pd.read_csv[r"C:\Users\hydro\a.txt", sep='\t']
firstCol = np.asarray[file.FirstColName]
print [firstCol]
answered Dec 12, 2017 at 14:51
SubhashiSubhashi
3,9571 gold badge22 silver badges21 bronze badges
Learning Objectives
In this challenge we are going to focus on accessing a text file in Python to read the content of the file line by line.
Challenge
Your first challenge consists of writing a Python script that will read the following text file, one line at a time and display the content of each line on screen.
My Playlist.txt
To read the content of a text file line by line we are going to use a for loop that will loop through and extract each line of the text file one at a time. It will stop looping only once the it will have reached the last line.
Check this
animation that explains this process:
When the text file uses a CSV format [Comma Separtred Values], each line contains several values separated using a special character, often a comma or a semi-colon. In this case we can use the split[] method to extract each value of each line and store them in a list.
Check this animation that explains this process further:
Solution
The solutions below contains 5 main steps:
- Step 1: Open the text file using the open[] function. This function takes two parameters: The name of the file [e.g. “playlist.txt”] and the access mode. In our case we are opening the file in read-only mode: “r”.
- Read through the file one line at a time using a for loop.
- Split the line into an array. This is because in this file each value is separated with a semi-column. By splitting the line we can then easily access each value [field] individually.
- Output the content of each field using the print method.
- Once the for loop is completed, close the file using the close[] method.
Main Access Modes when opening a file with the open[] function.
r | Opens a file in read only mode. This is the default mode. |
r+ | Opens a file for both reading and writing. |
w | Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist yet, it will create the new file for writing. |
w+ | Opens a file for both writing and reading. Overwrites the file if the file exists. If the file does not exist yet, it will create the new file for writing. |
a | Opens a file for appending. The file pointer is at the end of the file. So new data will be added at the end of the file. If the file does not exist, it creates a new file for writing. |
a+ | Opens a file for both appending and reading. The file pointer is at the end of the file. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. |
Your Challenge
Use what you have learnt in this blogbpost to complete these challenges:
My mp3 playlist[Reading a text file using Python] US Population[Reading a text file using Python]