How to split a section in a list? - python

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

Related

Problems with a list…

I am new in Python and I need to access a csv file that I have in a folder. This is not a problem:
open(‘C:\Users\Directory\companies.csv’)
I have tried this:
from csv import reader
# read csv file as a list of lists
with open('companies.csv', 'r') as read_obj:
# pass the file object to reader() to get the reader object
csv_reader = reader(read_obj)
# Pass reader object to list() to get a list of lists
list_of_rows = list(csv_reader)
print(list_of_rows)
OUTPUT
[['Company', 'Departments', 'Employess'],
['A', 'Marketing', '200'],
['B', 'Sales', '300’],
['C', 'HR', '15'],
['D', 'Logistics', '500'],
[‘A’, ‘Admin’, ‘15’],
[‘A’, ‘Research’, ‘30’],
[‘B’, ‘COffice’, ‘150’],
[‘C’, ‘Transporte, ‘200’],
[‘B’, ‘IT’, ‘200’],
[‘B’, ‘Gorg’, ‘12’]]
I am unable to obtain total number of Company ‘B’ departments and a new list that includes the number of departments in company A with more than 20 employees. I tried this:
new_list = [x for x in companies if Company = A' and 'Departments' > 20]
def total_elements(companies):
count = 0
for element in companies:
count += 1
return count
print("the total number is: ", total_elements(new_list))
Thanks in advance!

Python: loading txt file into 2d list of different types

I have a 2d list saved in a text file that looks like this (showing the first 2 entries):
('9b7dad', "text", 'http://imgur.com/gallery/SPdGm27', '1', 'A', 5)
('2b6ebj', 'text2', 'https://i.redd.it/lzft358csdi21.jpg', '1', 'B', 6)
How should this be loaded into a list? (so for example list[0][0] = '9b7dad', list[1][1] = 'text2' etc)
You could try this:
f = open(<your file path>)
result = [
[g.replace("'", "")
for g in l.strip('()\n').replace(' ', '').replace('"', '').split(',')]
for l in f.readlines()]
f.close()
Given a text file with each line in the form you've shown:
('9b7dad', "text", 'http://imgur.com/gallery/SPdGm27', '1', 'A', 5)
('2b6ebj', 'text2', 'https://i.redd.it/lzft358csdi21.jpg', '1', 'B', 6)
You can use Pandas which offers a more straightforward way to handle/manipulate different data types.
Import pandas and read in the file, here called 'stack.txt':
import pandas as pd
data = pd.read_csv('stack.txt', sep=",", header=None)
Returns only the list of list:
alist = data.values.tolist()
Print to check:
print(alist)
[['9b7dad', 'text', 'http://imgur.com/gallery/SPdGm27', '1', 'A', 5],
['2b6ebj', 'text2', 'https://i.redd.it/lzft358csdi21.jpg', '1', 'B', 6]]
If need to process columns:
for i in range(len(data.columns)):
if i == 0:
data[i] = data[i].map(lambda x: str(x)[1:])
data[i] = data[i].map(lambda x: str(x)[1:-1])
if i == 5:
data[i] = data[i].map(lambda x: str(x)[:-1])
data[i] = data[i].astype(int)
if 0 < i < 5:
data[i] = data[i].map(lambda x: str(x)[2:-1])
#!/usr/bin/env python
import sys
myList = []
for line in sys.stdin:
elems = line.strip('()\n').replace(' ', '').split(',')
elems = [x.strip('\'\"') for x in elems]
myList.append(elems)
print(myList[0][0])
print(myList[1][1])
To use:
$ python ./load.py < someText.txt
9b7dad
text2
Use int(), float(), or str() to coerce fields in elems to certain types, as needed. Use a try..except block to catch malformed input.
import ast
with open(file_name) as f:
content = f.readlines()
content = [list(ast.literal_eval(x)) for x in content]
How to read files:
In Python, how do I read a file line-by-line into a list?
More about eval:
Convert string representation of list to list
try this (converting tuple to list):
my_list = []
my_list.append(list('9b7dad', "text", 'http://imgur.com/gallery/SPdGm27', '1', 'A', 5))
my_list.append(list('2b6ebj', 'text2', 'https://i.redd.it/lzft358csdi21.jpg', '1', 'B', 6))
result is a list of lists i.e. a 2 dimensional list. You can easily modify the code to fetch a line at a time in a for loop and append it to the list. Consider using split(',') if it is a comma separated list instead of a tuple e.g.
mylist = []
with open(filename, 'r') as my_file:
for text in my_file.readlines()
my_list.append(text.split(','))

remove an extra empty list while splitting lines from file-Python

I am trying to open a file which has two sentences and split the the two sentences into a list of words. However, when I do that I also get an extra empty list, how can this be prevented?
x1=[]
fopen=open("testxxx",'r')
for line in fopen:
x=line.split()
x1.append(x)
print x1
I get:
[['i', 'have', 'a', 'dog'], ['i', 'have', 'a', 'cat'], []]
Use splitlines() to read all lines of the file into a list:
with open('filename.txt', 'r') as f:
mylist = f.read().splitlines()
Split the lines with this list comprehension:
mylist = [line.split() for line in mylist]
Use filter() to remove any empty list items:
mylist = filter(None, mylist)
You could also do all of that while you are reading the file:
mylist = []
with open('filename.txt', 'r') as f:
for line in f:
if line.strip():
mylist.append(line.split())
x1=[]
fopen=open("testxxx",'r')
for line in fopen:
x=line.split()
if x:
x1.append(x)
print x1
remove empty list :
x1= [x for x in x1 if x]
You can check if the read line is empty or not as you try splitting its words.
word_list = []
lines = []
with open('myfile.txt') as file:
# stripping the whitespaces as the blank line may contain whitespaces
lines = [line.strip() for line in file.readlines()]
# split to words those lines that are not empty strings
word_list = [line.split() for line in lines if line]
OR, with a one-liner
with open('myfile.txt') as file:
word_list = [line.split() for line in file.readlines() if line.strip()]
Note that empty string '' is falsy in python. So, naturally if line is a empty string, if line evaluates to False, and likewise for line.strip(). Take a look here for truth-value-testing in python.
You can use readlines:
with open('filename.txt', 'r') as f:
mylist = [line for line in f.readlines() if line.strip() != '']
Or:
with open('filename.txt', 'r') as f:
lines = []
for line in f.readlines():
if line.strip() != '':
lines.append(line)
Or:
with open('filename.txt', 'r') as f:
lines = filter(lambda x: x.strip() != '', f.readlines())

Fill a list from a file

I have a file which contains words separated by commas like:
tom,harry,ant,qqqq
aa,ww,rr,gg,aa,hh,ss
I would like to split each element separated by a comma and fill a list like this:
array=['tom','harry','ant','qqqq','aa','ww','rr','gg','aa','hh','ss']
So far I tried with:
array=list()
for i in open(filename):
element = i.split(',',len(i))
array.append(element)
When I print I obtain two problems:
for i in array
print i
I obtain ['tom','harry','ant','qqqq\n'] and ['qqqq','aa','ww','rr','gg','aa','hh','ss\n']
I would like to avoid the \n and to have a unique list as said before
with open('myFile.txt', 'r') as myFile:
array = myFile.read().replace('\n', ',').split(',')
for i in array:
print i
One liner:
with open('myFile.txt', 'r') as myFile: array = myFile.read().replace('\n', ',').split(',')
You should also avoid using names like array, list etc when assigning values. It's bad practice.
If you have any other questions send me a pm!
You can strip the line first to avoid the \n, and use extend instead of append:
for i in open(filename):
line = i.strip()
element = line.split(',')
array.extend(element)
Extend is used to add the elements to your array, instead of adding the array itself. The result would be:
['tom','harry','ant','qqqq','aa','ww','rr','gg','aa','hh','ss']
Instead of:
[['tom','harry','ant','qqqq'], ['aa','ww','rr','gg','aa','hh','ss']]
Since it looks like a comma separated file, i recommend you to use CSV module.
import csv
with open('file') as f:
csv_file = csv.reader(f)
L = []
for i in csv_file:
L.append(i)
print [i for j in L for i in j]
Output:
['tom', 'harry', 'ant', 'qqqq', 'aa', 'ww', 'rr', 'gg', 'aa', 'hh', 'ss']
Iterating file yield lines with newline. Strip the newline explicitly:
By Replacing following line:
element = i.split(',',len(i))
with:
element = i.rstrip().split(',',len(i)) # Remove trailing space charcters.
or
element = i.rstrip('\r\n').split(',',len(i)) # Remove CR / LF.
You could use a regex:
>>> import re
>>> re.split(r"[,\n]+", open(filename).read())
['tom', 'harry', 'ant', 'qqqq', 'aa', 'ww', 'rr', 'gg', 'aa', 'hh', 'ss']

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