Showing posts with label read files. Show all posts
Showing posts with label read 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.

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)

Wednesday, February 3, 2016

Good and Pythonic way to iterate over files in a folder



First, we get the path by some way, most of cases with an open folder dialog.

Then we make a list with all files in that folder.

This can be useful for batch analyze / read/ write files.


import os


path = 'c://temp'
onlyfiles = [ f for f in os.listdir(path) if os.path.isfile(os.path.join(path,f))]