How do i read a delimited text file in python?

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:

  1. 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”.
  2. Read through the file one line at a time using a for loop.
  3. 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.
  4. Output the content of each field using the print method.
  5. Once the for loop is completed, close the file using the close[] method.

Main Access Modes when opening a file with the open[] function.

ModeDescription
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]

How do I read a tab delimited text file in Python?

To read tab-separated values files with Python, we'll take advantage of the fact that they're similar to CSVs. We'll use Python's csv library and tell it to split things up with tabs instead of commas. Just set the delimiter argument to "\t" . That's it!

How do I read a separated file in Python?

Solution.
Step 1: Open the text file using the open[] function. ... .
Read through the file one line at a time using a for loop..
Split the line into an array. ... .
Output the content of each field using the print method..
Once the for loop is completed, close the file using the close[] method..

How do I read a plain text file in Python?

There are 6 access modes in python..
Read Only ['r'] : Open text file for reading. ... .
Read and Write ['r+']: Open the file for reading and writing. ... .
Write Only ['w'] : Open the file for writing. ... .
Write and Read ['w+'] : Open the file for reading and writing. ... .
Append Only ['a']: Open the file for writing..

How do I read a pipe delimited file in Python?

i have used the split string method, but it only prints "n" from the the first split column[name]. ... .
If you literally copy and paste my code into a text file and run it, you will see that it works. ... .
with open["abc.txt","r" ] as infile: data = infile.read[] fields = data.split['|'] print[data[0]].

Chủ Đề