473,387 Members | 1,844 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,387 software developers and data experts.

Searching and pulling data out of an Excel CSV created file

Greetings. I'm new to Python programming, but I'm coming along. I'm having an issue trying to pull data from a CSV file created by an Excel spreadsheet (save as... csv function).

What I need to do is search through a list (rows) of data for a specific match to one of the columns, then pull that row of data into some variables.

The information looks like this in the Excel Spreadsheet:


100,102,20,0,FC,0,0,0,C,0,0,0,140000
295,343,83,25,FC,3,1,0,C,D342,0,0,530019

The 3rd column (showing 20 in the first row and 83 in the second row) is the data that I am evaluating for a match, and if a match is found I need to pull all the data from the row and put it into individual variables for use in the script.

I could make the 3rd column the 1st column instead, if it will help speed up the search process or make it easier.


Any ideas?
Apr 15 '07 #1
7 13155
bartonc
6,596 Expert 4TB
Greetings. I'm new to Python programming, but I'm coming along. I'm having an issue trying to pull data from a CSV file created by an Excel spreadsheet (save as... csv function).

What I need to do is search through a list (rows) of data for a specific match to one of the columns, then pull that row of data into some variables.

The information looks like this in the Excel Spreadsheet:


100,102,20,0,FC,0,0,0,C,0,0,0,140000
295,343,83,25,FC,3,1,0,C,D342,0,0,530019

The 3rd column (showing 20 in the first row and 83 in the second row) is the data that I am evaluating for a match, and if a match is found I need to pull all the data from the row and put it into individual variables for use in the script.

I could make the 3rd column the 1st column instead, if it will help speed up the search process or make it easier.


Any ideas?
Expand|Select|Wrap|Line Numbers
  1. >>> sampleData = "100,102,20,0,FC,0,0,0,C,0,0,0,140000\n295,343,83,25,FC,3,1,0,C,D342,0,0,530019"
  2. >>> key = "20"
  3. >>> for line in sampleData.split('\n'):
  4. ...     data = line.split(',')
  5. ...     print data
  6. ...     if data[2] == key:
  7. ...         print 'found: value = %s' %key
  8. ...         
  9. ['100', '102', '20', '0', 'FC', '0', '0', '0', 'C', '0', '0', '0', '140000']
  10. found: value = 20
  11. ['295', '343', '83', '25', 'FC', '3', '1', '0', 'C', 'D342', '0', '0', '530019']
  12. >>> 
If you don't know how to iterate through a text file yet, just post back and we'll get you going.

Thanks for joining.
Apr 15 '07 #2
Thanks for the quick reply. I'm not sure if it makes a difference, but I'm trying to pull data from a CSV file, not a text file. Granted, it is probably the same thing, as I believe CSV files are really just text files created with commas separating the cell values, right?

Is this the most efficient way of doing this? In any event, I'll give it a try and see if I can get it to work... thanks!
Apr 15 '07 #3
Okay, here's the code I tried:
Expand|Select|Wrap|Line Numbers
  1.              csvFile = getComponent().filePath
  2.                             pos = csvFile.rfind('Components\\')
  3.                             csvFile = csvFile[:pos] + 'Scripts\\Cal Tables.csv'
  4.  
  5.                             myfile = open(csvFile)
  6.                             myfile.seek(0)
  7.  
  8.                             for line in myfile.split('\n'):
  9.                                 data = line.split(',')
  10.                                 print data
  11.                                 if data[2] == agv_O_Cal.value and data[3] == agv_O_Mod.value:
  12.                                     print 'found: value = %s' %agv_O_Cal.value, agv_O_Mod.value
  13.                                     agv_P1 = data[5]
  14.                                     agv_P2 = data[6]
  15.                                     agv_P3 = data[7]
  16.                                     agv_WS1 = data[9]
  17.                                     agv_WS2 = data[10]
  18.                                     agv_WS3 = data[11]
  19.  
  20.  
  21.                                     print agv_P1, agv_P2, agv_P3, agv_WS1, agv_WS2, agv_WS3
  22.  
  23.  
And the error code I got was AttributeError: 'file' object has no attribute 'split'


So, I don't think its going to work that way, or I totally botched it and coded it wrong... lol.
Apr 15 '07 #4
ghostdog74
511 Expert 256MB
The information looks like this in the Excel Spreadsheet:
100,102,20,0,FC,0,0,0,C,0,0,0,140000
295,343,83,25,FC,3,1,0,C,D342,0,0,530019
this should be what your csv file look like, or is this what you see in your Excel spreadsheet? pls show what your csv file looks like? If its the normal csv, where each field is separated by commas, then what barton showed you is the way to go about doing it. generally:
Expand|Select|Wrap|Line Numbers
  1. for line in open("yourcsv"):
  2.      line = line.split(",")
  3.      thirdcol = line[2]
  4.      if thirdcol == something: 
  5.           print "do something"
  6.  
Apr 15 '07 #5
bvdet
2,851 Expert Mod 2GB
Okay, here's the code I tried:
Expand|Select|Wrap|Line Numbers
  1.              csvFile = getComponent().filePath
  2.                             pos = csvFile.rfind('Components\\')
  3.                             csvFile = csvFile[:pos] + 'Scripts\\Cal Tables.csv'
  4.  
  5.                             myfile = open(csvFile)
  6.                             myfile.seek(0)
  7.  
  8.                             for line in myfile.split('\n'):
  9.                                 data = line.split(',')
  10.                                 print data
  11.                                 if data[2] == agv_O_Cal.value and data[3] == agv_O_Mod.value:
  12.                                     print 'found: value = %s' %agv_O_Cal.value, agv_O_Mod.value
  13.                                     agv_P1 = data[5]
  14.                                     agv_P2 = data[6]
  15.                                     agv_P3 = data[7]
  16.                                     agv_WS1 = data[9]
  17.                                     agv_WS2 = data[10]
  18.                                     agv_WS3 = data[11]
  19.  
  20.  
  21.                                     print agv_P1, agv_P2, agv_P3, agv_WS1, agv_WS2, agv_WS3
  22.  
  23.  
And the error code I got was AttributeError: 'file' object has no attribute 'split'


So, I don't think its going to work that way, or I totally botched it and coded it wrong... lol.
You were not that far off. I probably would go about it something like the following:
Expand|Select|Wrap|Line Numbers
  1. '''
  2. csv file contents:
  3. 100,102,20,0,FC,0,0,0,C,0,0,0,140000\n295,343,83,25,FC,3,1,0,C,D342,0,0,530019\n300,359,50,40,FC,2,2,0,D,0,0,0,366484
  4. '''
  5.  
  6. # read the csv file using readline() file method, yields a list of lines found in the file
  7. # use list comprehension to split the data on ','
  8. # dataList = [line.split(',') for line in open(csvData.csv).readlines()]
  9.  
  10. # I added another line of data for the example
  11. dataList = [['100', '102', '20', '0', 'FC', '0', '0', '0', 'C', '0', '0', '0', '140000'], \
  12.             ['295', '343', '83', '25', 'FC', '3', '1', '0', 'C', 'D342', '0', '0', '530019'],\
  13.             ['300', '359', '50', '40', 'FC', '2', '2', '0', 'D', '0', '0', '0', '366484']]
  14.  
  15. # These are your variables
  16. agv_O_Cal = 83
  17. agv_O_Mod = 25
  18.  
  19. # convert to string, data types must match
  20. key = map(str, (agv_O_Cal, agv_O_Mod))
  21.  
  22. for item in dataList:
  23.     if key == item[2:4]:
  24.         break
  25.  
  26. # item is the data line you want
  27. print item
Expand|Select|Wrap|Line Numbers
  1. >>> ['295', '343', '83', '25', 'FC', '3', '1', '0', 'C', 'D342', '0', '0', '530019']
Apr 15 '07 #6
Turns out the error was in the for statement. Once I took out the split component in the for statement, it worked fine.

Thanks everybody!

One more question,

for efficiency, how can I stop the file read once a match is found? I am constantly accessing the file, so closing it might not be the best idea. Would it be better just to leave it open?
Apr 15 '07 #7
ghostdog74
511 Expert 256MB

for efficiency, how can I stop the file read once a match is found?
if you found the match during the iteration and wants to stop, you can use the 'break' keyword. eg
Expand|Select|Wrap|Line Numbers
  1. ...
  2. for line in open("file"):
  3.      if "something" in line:
  4.           print "do something here"
  5.           break
  6.  
Apr 15 '07 #8

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

Similar topics

9
by: Ksenia Marasanova | last post by:
Hi, I have a little problem with encoding. Was hoping maybe anyone can help me to solve it. There is some amount of data in a database (PG) that must be inserted into Excel sheet and emailed....
5
by: Pai | last post by:
Hello there, I have and small excel file, which when I try to import into SQl Server will give an error "Data for source column 4 is too large for the specified buffer size" I have four...
1
by: bryanilton | last post by:
Hi guys, I hope that one of you gurus can help me out here. Basically what I'd like to do is be able to price harddrives. I'd like to have two drop-down boxes. One for the manufacturer and the...
4
by: washoetech | last post by:
Hello, I am working on a project where I need to be able to grab the data from an Excel spreadsheet and create a new table in my database based on the columns in the spreadsheet. After the...
1
by: david shapiro | last post by:
I've created an excel workbook with several worksheets. In each worksheet, I've highlighted a number of the cells containing key statistics in red. Is there any easy one-step way that I can pull...
1
by: Usarian Skiff | last post by:
I'm pulling a list of data from an excel file. When I open Excel files directly, if someone else has it open, I can select 'READ-ONLY' from a pop-up. When accessing the file from within my...
3
by: Bharathi Kumar | last post by:
Hi, In my windows application, I have created excel.application object and read the excel data. I saved the data to sql server also. The problem Iam facing is when I create an instance of...
0
by: MKH | last post by:
Greetings, OS = Windows 2000 Server SP4 SQL = MS SQL 2000 Server SP4 Language = T-SQL I have an excel file stored in a data base table as an image data type. I would like to somehow...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.