Python txt file join dictionary and query method
As shown below:
dicFile = open('train_1.txt', 'r')# Open the data print 'Starting to load data...' txtDict = {}#Build a dictionary while True: line = () if line == '': break index = ('\t')#Split by tab key key = line[:index] value = line[index:] txtDict[key] = value# Add to dictionary () ## Look up the dictionary srcFile = open('', 'r')# Key to match destFile = open('', 'w')# Write in the dictionary that matches while True: line = () if line == '': break index = (' ') key = line[:index] if txtDict.has_key(key): (key) (txtDict[key]) else: (key) ('\n') print 'All done!' () ()
Above this Python txt file to join the dictionary and query is all I have to share with you, I hope to be able to give you a reference, and I hope you support me more.
Related articles
Python implementation of the web page screenshot function [PyQt4 and selenium components].
This article introduces the Python implementation of the web page screenshot function, combined with examples of the form were described using PyQt4 components and selenium components for the web page screenshot operation of the relevant implementation techniques and precautions, the need for friends can refer to the following2018-07-07A python implementation of thread pooling
This article introduces the python thread pool method, example analyzes the principle of Python thread pool and related implementation techniques, need friends can refer to the following2015-06-06Summary of common code snippets for Pytorch experiments
This article introduces the Pytorch experiments commonly used code segment summary, the text through the sample code is very detailed, for everyone to learn or work with certain reference learning value, you can refer to the following friends2020-11-11Easy date and time handling with the python dateutil library
This article introduces the use of python dateutil library to easily handle the date and time instances, there is a need for friends can refer to reference, I hope to be able to help, I wish you more progress, early promotion and salary increase!2024-01-01Getting Started with Python's GPU-accelerated parallel computing based on pyCUDA.
This article introduces Python based on pyCUDA to achieve GPU-accelerated parallel computing, combined with an example of the form of analysis of Python using pyCUDA for GPU-accelerated parallel computing principles and related implementation techniques, friends can refer to the need for the following2018-06-06Python function parameter basics and examples
When declaring a function, you will generally decide whether the function needs parameters or not, depending on what the function is trying to accomplish. In most cases, the functions we declare use parameters, and this article focuses on Python function parameters2022-08-08The difference between a single underscore and a double underscore in python.
This article introduces the python inside the single underscore and double underscore difference, I think it's pretty good, now share it with you, but also to give you a reference. Follow the editor over to see it together2017-12-12Using xadmin under python 3.5 and fixing source code bugs
xadmin is based on Python and Django management framework, want to be able to skillfully use, learn Django is a must. The following article introduces the use of xadmin under python 3.5 and when we rewrite Django's User table, Django will be a bug problem, the need for friends can refer to the next.2017-05-05Explaining python data structures and algorithms in detail
This article introduces the python data structures and algorithms, the text through the sample code is very detailed, for everyone's learning or work has a certain reference learning value, the need for friends below with the editor to learn together!2019-04-04Python implementation of a simple countdown function
This article is mainly for you to achieve a detailed introduction to the python simple countdown function, the sample code in the text is very detailed, with certain reference value, interested partners can refer to it!2021-04-04