Python CSV read a single column as a list - python

I have this code:
type(i[0]) == 'str'. How can I get it as a list? and later get print(i[0][0]) == 'a'
import csv
i = [['a', 'b'], 'c', 'd']
with open('a.csv', 'a', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(i)
file.close()
with open('a.csv', 'r', newline='', encoding='utf-8') as file:
for i in csv.reader(file, delimiter=';'):
print(type(i[0]))

The reason it is returning str instead of list because the csv writer will always do the equivalent of str(['a','b']), which is "['a', 'b']",c,d, so you can use ast.literal_eval to get the list from the string list like this for the first element-
import csv
import ast
i = [['a', 'b'], 'c', 'd']
with open('a.csv', 'a', newline='', encoding='utf-8') as file:
writer = csv.writer(file, delimiter=';')
writer.writerow(i)
file.close()
with open('a.csv', 'r', newline='', encoding='utf-8') as file:
for i in csv.reader(file, delimiter=';'):
i[0] = ast.literal_eval(i[0])
print(type(i),type(i[0]), i[0][0])

You can use a for-loop. Like this:
example a.csv
hello, aloha, hola
bye, aloha, adios
python code:
import csv
col1 = []
with open('a.csv') as file:
r = csv.reader(file)
for row in r:
col1.append(row[0])
print(col1) # => ['hello', 'bye']
Alternatively, you could also use a list comprehension:
import csv
col1 = []
with open('a.csv') as file:
r = csv.reader(file)
col1 = [row[0] for row in r]
print(col1) # => ['hello', 'bye']

Related

How to write a table line by line with for loop

I have different files in where I pick up some data/values in order to produce a table grouping everything.
Here is a small example of the code I am using:
stations = ["AAA", "BBB", "CCCC", "DDDD"]
datadir = "/home/data/"
table = []
for station in stations:
os.chdir(datadir)
nc = Dataset(station + ".nc", 'r+')
p = (nc.variables['Rainf'][:,0,0]
evap = nc.variables['Qle'][:,0,0]
table.append(p)
table.append(evap)
table_t=list(table)
with open (datadir + "table.csv", 'w') as ofile:
writer = csv.writer(ofile)
writer.writerow(table_t)
But this code only write all the results of all stations in one row. What do I need to change in order that for each station the code write the data/values on the next row?
You want to use writer.writerows(table_t) instead.
writerows() method takes the iteration and creates lines per item in the list.
Example:
data = [list(i) for i in 'abcde fhghi jklmn opqrs'.split()]
# [['a', 'b', 'c', 'd', 'e'],
# ['f', 'h', 'g', 'h', 'i'],
# ['j', 'k', 'l', 'm', 'n'],
# ['o', 'p', 'q', 'r', 's']]
with open('test.csv','w') as file:
writer = csv.writer(file, lineterminator='\n')
writer.writerows(data)
# test.csv
# a,b,c,d,e
# f,h,g,h,i
# j,k,l,m,n
# o,p,q,r,s
you need to iterate over the table you want to write out:
with open (datadir + "table.csv", 'w') as ofile:
writer = csv.writer(ofile)
for row in table:
writer.writerow(row)
hope that helps.

Read text from file and rearrange the columns?

I have a text file which has content:
A;B;C;D
a;b;c;d
I am reading the file in python and wants to rearrange the column such as-
B;C;D;A
b;c;d;a
I have tried reading column -
file = open('add.txt','r')
text = file.read()
column1 = x[0] for x in text
But this didn't help. So I need a way to convert it.
You can use the csv module to read your file into lists, then is just a matter of re-indexing the list order.
import csv
list_variable = []
with open("file.txt") as add_txt:
reader = csv.reader(add_txt, delimiter =';')
for line in reader:
line += [line.pop(0)]
list_variable.append(line)
You can use tuple/iterable unpacking to do this
with open('add.text','r') as f:
for line in f:
cols = line.strip().split(';')
col1, col2, col3, col4 = cols
print col2, col3, col4, col1
#output
B C D A
b c d a
You don't need to use the csv module as one answer suggested:
with open('add.txt', 'r') as file:
text = file.read()
result = []
for line in text.splitlines():
cols = line.split(';')
cols = cols[1:] + [cols[0]]
result.append(cols)
print(result)
Output:
[['B', 'C', 'D', 'A'], ['b', 'c', 'd', 'a']]

How to split a section in a list?

Okay so I have a list called names and there are other words in the list but this is the result of names[0]
Chen,David,M,334791530,,11Z,,16770712,,,,,,00015956753,
Chen,Peter,M,321564726,,11B,,19979810,,,,,,00012446698,
Chung,Rowan,M,32355988,,11T,,17890708,,,,,,00012127821,
Chung,Kyle,M,387638355,,10U,,19970317,,,,,,00015604870,
Fan,Mark,M,34217543,,10U,,19707713,,,,,,00015799079,
How do I split names[0] so that it comes out with just the last name, first name, and gender?
Here's the rest of my code:
file = open('CASS.txt', 'r')
f = file.readlines()
file.close()
for line in f:
if line.find('ICS3M105')>=0:
names = line.split()
for name in names[0]:
if name in range(0,1):
print(names)
for line in f:
names = line.split()
print names[0].split(',')[0:3]
with open('CASS.txt', 'r') as f:
for line in f:
name_last, name_first, gender = line.split(',')[0:3]
Or using the csv module which will may be more reliable for upcoming tasks
import csv
with open('CASS.txt', 'r') as f:
for row in csv.reader(f):
name_last, name_first, gender = row[0:3]
>>> s = """Chen,David,M,334791530,,11Z,,16770712,,,,,,00015956753,
Chen,Peter,M,321564726,,11B,,19979810,,,,,,00012446698,
Chung,Rowan,M,32355988,,11T,,17890708,,,,,,00012127821,
Chung,Kyle,M,387638355,,10U,,19970317,,,,,,00015604870,
Fan,Mark,M,34217543,,10U,,19707713,,,,,,00015799079,"""
You can use a list comprehension to split on commas, then use slicing to index element [0] to [2] (inclusive) of the split operation.
>>> [i.split(',')[:3] for i in s.split('\n')]
[['Chen', 'David', 'M'],
['Chen', 'Peter', 'M'],
['Chung', 'Rowan', 'M'],
['Chung', 'Kyle', 'M'],
['Fan', 'Mark', 'M']]

How to open each column of a text file as separate list?

How to open each column of a text file as separate list?
import csv
infile = open ('data.txt', 'r')
reader = csv.reader(infile, delimiter=' ')
col1 = [line[0] for line in reader]
print (col1)
col2 = [line[1] for line in reader]
print (col2)
But column2 is empty. Any idea???
The data.txt look as follows:
a d 1
b e 2
c f 3
In your code col2 is empty because after the first iteration(List comprehension for col1) the iterator is already exhausted.
You can zip with *:
import csv
with open('abc.csv') as f:
reader = csv.reader(f, delimiter=' ')
c1, c2, c3 = zip(*reader)
print c1
print c2
print c3
Output:
('a', 'b', 'c')
('d', 'e', 'f')
('1', '2', '3')
with open ('data.txt','r') as infile:
col1= [i.strip().split(' ')[0] for i in infile]
with open ('data.txt','r') as infile:
col2= [i.strip().split(' ')[1] for i in infile]
print (col1)
print (col2)

Cannot return list elements from Python function

I have a function, table(), that reads a csv file and returns the rows in individual lists. I want to map these lists to create a dictionary, with field headers being the keys and the underlying rows being the values. I cannot seem to do this however. When I try to call only the first element within the list of lists I created from the function ( l) in the command prompt, it returns all the lists up to 'N', the first letter in the word 'None', despite me breaking (return) if reader is None. When I do the same with a sys.stdout to a text file, it does the same, but the 'N' is replaced with <type 'list'>. Does anyone know what I'm doing wrong, and how I can go about creating a dictionary (or a list of columns, for that matter) from a CSV file given my table() function?
import csv
def table():
with open('C:\\Python27\\test.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
print row
if reader is None:
return
l = list(str(table()))
keys = l[0]
print keys
Output text file:
['Field1', 'Field2', 'Field3', 'Field4']
['a', '1', 'I', 'access']
['b', '2', 'II', 'accessing\n']
['c', '3', 'III', 'accommodation']
['d', '4', 'IIII', 'basically']
<type 'list'>
More pythonically
def table():
with open('C:\\Python27\\test.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
yield row
You don't actually return anything from the table function. Try it like this:
def table():
with open('C:\\Python27\\test.csv', 'rb') as f:
lines = []
reader = csv.reader(f)
if reader:
for row in reader:
lines.append(row)
return lines
else:
return []

Categories

Resources