SoFunction
Updated on 2024-11-15

Python txt file join dictionary and query method

Python txt file join dictionary and query method

Updated January 15, 2019 09:59:52 by Whosoever Blogs
Today, I'd like to share with you a Python txt file to join the dictionary and query method, has a good reference value, I hope to help you. Together follow the editor over to see it

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.

  • Python
  • txt
  • dictionaries
  • consult (a document etc)

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 following
    2018-07-07
  • A 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 following
    2015-06-06
  • Summary 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 friends
    2020-11-11
  • Easy 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-01
  • Getting 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 following
    2018-06-06
  • Python 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 parameters
    2022-08-08
  • The 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 together
    2017-12-12
  • Using 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-05
  • Explaining 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-04
  • Python 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

Latest Comments