Showing posts with label text files. Show all posts
Showing posts with label text files. Show all posts

Tuesday, April 20, 2021

Pandas - Reading CSV file with variable number of columns

 If the CSV/ text file has different number of columns along the rows, it will fail reading with simple "pd.read_csv(file)".

Instead of it, try naming the columns. It will read until the number of columns supplied.

For example:

df0=pd.read_csv(filename, sep=';',names=['a', 'b', 'c', 'd', 'e'])

It will read only until 5 columns wide.

Tuesday, July 24, 2018

How to extract all numbers / floats from a string with python regex

import re

stringWithNumbers='anystring2with 6.89 numbers3.55 3.141312'
digits = re.findall("[-+]?\d+\.?\d*", stringWithNumbers)

Friday, June 8, 2018

CSV file to Python list of lists

import csv

with open('file.csv', 'r') as f1:
    reader = csv.reader(f1)
    your_list = list(reader)

Friday, November 13, 2015

Reading Text files in Python

This is a basic funcionality, but very useful for many applications.

It opens a pure text file. You do not have to import any extra library.

The main sintax for opening files is:

file_object = open(file_name [, access_mode][, buffering])

where:
file_object -> variable
file_name -> the file you want to open
access_mode -> 'r' for read, 'rw' for read and wite text files
buffering -> size of data you read from file. If you leave this blank, it uses the system default - recommended.

To read only text files, it becomes:

file = open('yourfile.txt', 'r')

'yourfile.txt' must contain the correct folder address to it.
'r' stands for read only.

If you want all the contents of your file in one string, you can call read() method:

print file.read()

A good way to deal with text files is to turn the text into a list, each line as an list element. To do this, use the readlines() method:

lstLines = file.readlines()

After all the readings and processings you want to do with the file, you have to close it as following:

file.close()