Reputation:
I got the following code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
#read information
f = open ("/home/ibrahim/Desktop/Test.list")
text = f.read()
#show existing companys
Companyname = text.split("\n")
print Companyname
#User chooses a company he wants to know more about
raw_input('\n<Wählen Sie die Firma Ihrer Wahl aus um die Informationen anzuzeigen.>\n\n<Geben Sie die Firmenspezifische Zahl ein und b$
#Companyspecific information is getting revealed
The Test.list looks like this
(1)Chef,1965,10
(2)Fisher,1932,20
(3)Gardener,1998,5
My aim is, that the user of this program can choose a specific Company he wants to find out more about. For example, the year this company started and the number of employees
Example: Companyname = Chef, year the company started = 1965 and number of employees = 10
I don't want to print more than the company's name, because the information in the future will contain more than just the founding year and the number of employees.
Edit: Sucks that I can't accept every answer and can't up-vote anybody because I really want to :S I appreciate every help I got from you and the guy who edited my post so it looked a bit better ^^
Upvotes: 3
Views: 190
Reputation: 711
you can do:
info = text.split("\n")
CompanyName = [inf.split(')')[1].split(',')[0] for inf in info]
Upvotes: 1
Reputation: 195
Here is an other example, more efficient.
#!/usr/bin/env python
# virtualenv ~/.virtualens/company_reader
# source ~/.virtualenvs/company_reader/bin/activate
# pip install prettytable
# python reader.py
import re
from collections import namedtuple
# PrettyTable is a tool to format a list of elements with an elegant table.
import prettytable
Company = namedtuple('Company', ['identifier', 'name', 'year', 'nbr_employee'])
company_name = raw_input('Enter the company: ').lower()
# The regexp
pattern_str = r"\((?P<identifier>\d+)\)(?P<name>\w+),(?P<year>\d+),(?P<nbr_employee>\d+)"
pattern = re.compile(pattern_str)
companies = []
# TODO: add a argument parser
# docopt is a correct solution for this purpose
for line in open('data.txt'):
matching = pattern.match(line)
# if there is no matching, continue on the next line
if matching is None:
continue
company = Company(*matching.groups())
if company.name.lower() == company_name:
companies.append(company)
if not companies:
print "Sorry, there is no result"
else:
pt = prettytable.PrettyTable(['Identifier', 'Name', 'Year', 'Number of Employee'])
pt.align['Identifier'] = 'l'
pt.align['Name'] = 'l'
pt.align['Number of Employee'] = 'r'
for company in companies:
pt.add_row([company.identifier, company.name, company.year, company.nbr_employee])
print pt
Upvotes: 1
Reputation: 3504
You will done it with csv reader. http://docs.python.org/2/library/csv.html
>>> import csv
>>> list={}
>>> with open('/home/ibrahim/Desktop/Test.list', 'rb') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=',')
... for name,year,number in spamreader:
... id,name = name.split(')',1)
... list[name] = {'year':year,'number':number,'name':name}
>>> name = raw_input('Enter company name')
>>> print '{name} started at {year} and has {number} workers'.format(**list[name])
Upvotes: 0
Reputation: 195
Here is an working example:
#!/usr/bin/env python
for line in open('data.txt'):
company, year, number_of_employee = line.split(',')
print "Company: %s" % company
Regards,
Upvotes: 2
Reputation: 82899
How about using a regular expression to find the several parts?
import re
with open("Test.list") as f:
for line in f.readlines():
m = re.match(r'\((\d)+\)([^,]+),(\d+),(\d+)', line)
print m.groups()
The first group is the ID (\d)+
, the second the name ([^,]+)
(everything except a comma), the third the year (\d+)
and the fourth the number of employees (\d+)
.
Of course, if it's okay to have the company name together with the ID you can just as well just use line.split(',')
or csv
.
Upvotes: 1