| | |
Combining txt from 2 files
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 4
Reputation:
Solved Threads: 0
Alright this is my first program in python, so be gentle. I have 2 files I need to basically combine to create a record in mysql. Features.txt has around 1300 records and property.txt has about 25,000 records. I can read in property.txt fine and get my insert statements. My problem is trying to read in features.txt. I only need the lines that have field 1 = RES. I was thinking a dictionary ('1701' : 'Central Air', '1702' : 'Window Unit", etc) but can seem to quite get it.
I'm inserting into a mysql database here is what I have right now:
but I need
Also, once I get the features.txt into a dictionary how do I do the substitution?
python Syntax (Toggle Plain Text)
for featureline in ffeaturefile.readlines(): featureline = featureline.strip().split("|") addlist += "'%s', '%s'," % (featureline[0], featureline[3]) resfeaturelist = dict(addlist)
Python Syntax (Toggle Plain Text)
features.txt 1701|RES|HVAC|Central Air 1702|RES|HVAC|Window Unit 1703|RES|HVAC|Heat Pump - AC| 1704|RES|HVAC|No A/C 1705|RES|HVAC|Wall Unit - AC 1706|RES|HVAC|Multizone A/C 1708|RES|HVAC|Gas Hot Air 1701|CND|HVAC|Central Air ... property.txt (there are a 105 fields so I shortened it here) 0.293|53548|47459||4|101|QUART|2|4||Matthews||1505,1510,1514||1701,1708,1722| ...
I'm inserting into a mysql database here is what I have right now:
Python Syntax (Toggle Plain Text)
Insert into openrealty_en_listingsdbelements (listingdbelements_field_name,listingdbelements_field_value,listingsdb_id,userdb_id) values ('HVAC','1701,1708','2','0');
but I need
Python Syntax (Toggle Plain Text)
Insert into openrealty_en_listingsdbelements (listingdbelements_field_name,listingdbelements_field_value,listingsdb_id,userdb_id) values ('HVAC','Central Air,Gas Hot Air','2','0');
python Syntax (Toggle Plain Text)
for number, resline in enumerate(fresfile): resline = resline.strip().split("|") for count in [0, 21]: if (count == 21): #change values to text in features.txt resfields = resline[count].split(",") for i, item in enumerate(resfields): if (featureline[0] == item): print resfields[3] else: fsqlfile.write ("Insert into openrealty_en_listingsdbelements (listingdbelements_field_name,listingdbelements_field_value,listingsdb_id,userdb_id) values ('" + resheader[count] + "','" + resline[count] + "','" + str(number) + "','0');\n")
Also, once I get the features.txt into a dictionary how do I do the substitution?
•
•
•
•
python Syntax (Toggle Plain Text)
for featureline in ffeaturefile.readlines(): featureline = featureline.strip().split("|") addlist += "'%s', '%s'," % (featureline[0], featureline[3]) resfeaturelist = dict(addlist)
python Syntax (Toggle Plain Text)
for number, resline in enumerate(fresfile): resline = resline.strip().split("|") for count in [0, 21]: if (count == 21): #change values to text in features.txt resfields = resline[count].split(",") for i, item in enumerate(resfields): if (featureline[0] == item): print resfields[3] else: fsqlfile.write ("Insert into openrealty_en_listingsdbelements (listingdbelements_field_name,listingdbelements_field_value,listingsdb_id,userdb_id) values ('" + resheader[count] + "','" + resline[count] + "','" + str(number) + "','0');\n")
Also, once I get the features.txt into a dictionary how do I do the substitution?
I propose the following modification to the first bit of code:
python Syntax (Toggle Plain Text)
resfeaturelist = {} for featureline in ffeaturefile.readlines(): featureline = featureline.strip().split("|") addlist += "'%s', '%s'," % (featureline[0], featureline[3]) resfeaturelist[ featureline[0] ] = featureline[3]
python Syntax (Toggle Plain Text)
>>> dd = {} # Initialize the dictionary >>> dd[ '7801' ] = 'Heat Pump' >>> dd[ '7902' ] = 'Central A/C' >>> dd[ '8901' ] = 'Box Fan' >>> print dd[ '7801' ] Heat Pump >>> "INSERT x into y VALUES( '%s', '%s', '0' );" % ( dd['7801'], dd['8901'] ) "INSERT x into y VALUES( 'Heat Pump', 'Box Fan', '0' );" >>> dd.get( '7901' ) >>> dd.get( '7902' ) 'Central A/C' >>>
get() is safer since you can check if <value> == None: so that you don't get any code-stopping exceptions; however in your case you may want the code to stop.Let me know whether this clears up your questions, and whether that first bit helped to alleviate your problems..
Last edited by jlm699; Nov 10th, 2008 at 10:47 am.
•
•
Join Date: Nov 2008
Posts: 4
Reputation:
Solved Threads: 0
That's perfect. I think I was originally just creating a list. I could find anything good for docs on dictionarys (or python for that matter) online. The dictionary is great, and I can see how to use it on one item. How can I do it for multiple items? Also, 1 record might have 1 and another might have 10.
my code right now is just
Which prints out:
['1701', '1708', '1722']
['1701', '1708']
['1701', '1708']
['1701', '1708', '1706']
which I need to transform into:
['Central Air', 'Gas Hot Air', 'Woodstove']
['Central Air', 'Gas Hot Air']
['Central Air', 'Gas Hot Air']
['Central Air', 'Gas Hot Air', 'Multizone A/C']
I'm sure it simple. I tried just
Which just gives me an error.
Do you know of a good syntax reference?
Thanks!
Chris
my code right now is just
python Syntax (Toggle Plain Text)
if (count == 21): resfields = resline[count].split(",") print resfields
Which prints out:
['1701', '1708', '1722']
['1701', '1708']
['1701', '1708']
['1701', '1708', '1706']
which I need to transform into:
['Central Air', 'Gas Hot Air', 'Woodstove']
['Central Air', 'Gas Hot Air']
['Central Air', 'Gas Hot Air']
['Central Air', 'Gas Hot Air', 'Multizone A/C']
I'm sure it simple. I tried just
python Syntax (Toggle Plain Text)
if (count == 21): resfields = resfeaturelist [resline[count].split(",")] print resfields
Do you know of a good syntax reference?
Thanks!
Chris
•
•
Join Date: Mar 2007
Posts: 110
Reputation:
Solved Threads: 31
If I understand you correctly, you want to substitute the "VALUE" of a residence feature which would be a "KEY" in a dictionary. For each different value, there will be a different key. A dictionary is perfect for this. Example: Both print:
>>> ['Central Air', 'Multizone A/C', 'Woodstove']
Python Syntax (Toggle Plain Text)
# features = open('features.txt').read() features = '''1701|RES|HVAC|Central Air 1702|RES|HVAC|Window Unit 1703|RES|HVAC|Heat Pump - AC| 1704|RES|HVAC|No A/C 1705|RES|HVAC|Wall Unit - AC 1706|RES|HVAC|Multizone A/C 1708|RES|HVAC|Gas Hot Air 1722|RES|HVAC|Woodstove 1701|CND|HVAC|Central Air''' featuresDict = {} for item in features.split('\n'): if 'RES' in item: items = item.split('|') featuresDict[items[0]] = items[3] resfield = ['1701','1706','1722'] # as a function, retain original list def sub_features(s): return [featuresDict[item] for item in s[:]] print sub_features(resfield) # OR # modify original list for i, item in enumerate(resfield): resfield[i] = featuresDict[item] print resfield
>>> ['Central Air', 'Multizone A/C', 'Woodstove']
Here's a good one: Python docs
Also, in the future it helps if you post the traceback of your error message instead of just saying "this gives me an error". It'll help the community debug your problem !
Also, in the future it helps if you post the traceback of your error message instead of just saying "this gives me an error". It'll help the community debug your problem !
![]() |
Similar Threads
Other Threads in the Python Forum
- Previous Thread: sending information with a wsdl file
- Next Thread: New to pytong and need help with a database connection
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog decimals dictionaries dictionary directory drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






