reading a file in python
44,476
Solution 1
For simple tasks, you can just use the str.split()
method. split()
takes the delimiter as its parameter, but splits on whitespace if none is given.
>>> lin="a b c d"
>>> lin.split()
['a', 'b', 'c', 'd']
Solution 2
Does the same technique for csv modules does not work?
import csv
reader = csv.reader(open("filename"), delimiter="\t")
Delimiter can be "\s" or "\t".
You can also use DictReader this way:
f = open(filename, '')
try:
reader = csv.DictReader(f)
for row in reader:
print row
finally:
f.close()
you can also use brute force technique
for line in open(filename):
listWords = line.split("\t")
Split function:
>>> t = 'ID YR MO DA YrM MoM DaM'
>>> t.split(" ")
['ID', 'YR', 'MO', 'DA', 'YrM', 'MoM', 'DaM']
For calculating no of days, use datetime module : http://docs.python.org/library/datetime.html
>>> import datetime
>>> k = datetime.date(2010, 05, 26) - datetime.date(2010, 02, 10)
>>> k.days
105
>>>
Author by
user458858
Updated on July 10, 2022Comments
-
user458858 almost 2 years
I am new to python been using it for graphics but never done it for other problems. My question is how to read this file which is tab or space delimited and has headers in python, i know how to do comma delimted file but not done this on?
ID YR MO DA YrM MoM DaM 100 2010 2 20 2010 8 2010 30 110 2010 4 30 2010 9 2010 12 112 2010 8 20 2010 10 2010 20
Also is there a way to find the difference of number of days between two dates.
-
user458858 over 13 yearsso if i use csv reader how do i access the each element of each row from the input file.
-
PaulMcG over 13 yearsuse a csv.DictReader and the header row will be used to read the keys for each respective value in the following rows, and then each row will return a dict with the row's values as the values of the row's dict.
-
sykora over 13 yearsGreat, never knew that I could use csv with non-csv files. But using delimiter="\s" doesn't seem to work.
-
martineau over 13 years@sykora: Using a "\s" would only work if the two character string "\s" was literally between each value in the file you tried to read -- which is very unlikely. Besides "\t" (tab), the most common delimiter (and the default) is "," (a comma).
-
sykora over 13 years@martineau: pyfunc's answer says "Delimiter can be '\s' or '\t'". I was merely wondering why he includes '\s'. I tried it out, and indeed what you say is right.
-
martineau over 13 years+1 for a comprehensive answer addressing both parts of question.