473,395 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Python - csv

26
I am trying to write a program in python 2.6 which does the following:
prompts for input
selects that item in a csv file
displays only the data for that input

There are a total of 5 locations
A1, A2, A3,A4,A5

---
I can get the input, and I can read the whole csv file, then it prints the date, location, and course, for all of the locations, I only require the "date", "location", and "course" for that input

The input of say "A1" should only display the following:
Date Location Course
4-4-10 A1 web1
5-4-10 A1 web2
6-4-10 A1 web3
---------

csv file - test.txt
Date,Location,Course,Stage
4-4-10,A1,web1,1
4-4-10,A2,wev2,2
5-4-10,A1,web2,2
6-4-10,A1,web3,3
Apr 24 '10 #1
5 2120
bvdet
2,851 Expert Mod 2GB
You can test for membership with the in operator to tabulate the required data. Then it's a matter of formatting the output.
Expand|Select|Wrap|Line Numbers
  1. fn = "location.txt"
  2. location = "A1"
  3. output = []
  4. f = open(fn)
  5. labels = f.readline().strip().split(',')
  6. for line in f:
  7.     lineList = line.strip().split(',')
  8.     if location in lineList:
  9.         output.append(lineList)
  10. print
  11. print "%s%s%s" % (labels[0].center(8),
  12.                   labels[1].center(10),
  13.                   labels[2].center(8))
  14. print "\n".join(["%s%s%s" % (s[0].center(8),
  15.                              s[1].center(10),
  16.                              s[2].center(8)) for s in output])
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> 
  2.   Date   Location  Course 
  3.  4-4-10     A1      web1  
  4.  5-4-10     A1      web2  
  5.  6-4-10     A1      web3  
  6. >>> 
Apr 24 '10 #2
ozchadl
26
Thanks for your help.


It the csv file - test.txt had say "a1" and "A1", and the input was say "a1" it would only display that item for "a1" instead of both.

When the file is being read, would this need to be converted to upper or lowercase for it to display the item for "a1" and "A1"
Apr 25 '10 #3
bvdet
2,851 Expert Mod 2GB
Here are two ways to approach it:
Convert location to upper case and line to upper case before splitting
Check for membership of location upper and lower case in lineList
Apr 25 '10 #4
ozchadl
26
Could you tell me how I write the data displayed from the original reply to a csv text file called output.txt
If the input was a1, it woudl display this on screen and write it to a csv text file:
Date Location Course
4-4-10 A1 web1
5-4-10 A1 web2
6-4-10 A1 web3
Apr 26 '10 #5
bvdet
2,851 Expert Mod 2GB
The output you request would not be a CSV file, but an ascii text file. To write the output string shown in my example to a file:
Expand|Select|Wrap|Line Numbers
  1. outputList = []
  2. outputList.append("%s%s%s" % (labels[0].center(8),
  3.                               labels[1].center(10),
  4.                               labels[2].center(8)))
  5. outputList.append("\n".join(["%s%s%s" % (s[0].center(8),
  6.                                          s[1].center(10),
  7.                                          s[2].center(8)) for s in output]))
  8.  
  9. f = open("output.txt", 'w')
  10. f.write("\n".join(outputList))
  11. f.close()
Apr 26 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.