python 读取指定文本并拼接成指定的格式

# -*- coding: utf-8 -*-
import os def getHelloWorld(path, fileName):
"""
:param path:
:param fileName:
:return: string
"""
try:
os.path.isdir(path)
except Exception as e:
raise e
else:
resHello = ''
resWorld = ''
absPath = os.path.abspath(path)
if absPath.endswith("\\"):
filePath = absPath.replace('\\','\\\\') + fileName
if os.path.isfile(filePath):
with open(filePath)as f:
txt = f.readlines()
resHello = txt[0].split('_')[0]
resWorld = txt[1].split(' ')[0]
return resHello + resWorld
else:
filePath = (absPath + ' ').replace(' ','\\\\') + fileName
if os.path.isfile(filePath):
with open(filePath)as f:
txt = f.readlines()
resHello = txt[0].split('_')[0]
resWorld = txt[1].split(' ')[0]
return resHello + resWorld def getHelloAndChina(path, fileName):
"""
:param path:
:param fileName:
:return: string
"""
try:
os.path.isdir(path)
except Exception as e:
raise e
else:
resHello = ''
resWorld = ''
absPath = os.path.abspath(path)
if absPath.endswith("\\"):
filePath = absPath.replace('\\', '\\\\') + fileName
if os.path.isfile(filePath):
with open(filePath)as f:
txt = f.readlines()
resHello = txt[0].split('_')[0]
resWorld = txt[1].split(' ')[1].split('_')[0]
return resHello + resWorld
else:
filePath = (absPath + ' ').replace(' ', '\\\\') + fileName
if os.path.isfile(filePath):
with open(filePath)as f:
txt = f.readlines()
resHello = txt[0].split('_')[0]
resWorld = txt[1].split(' ')[1].split('_')[1]
return resHello + resWorld if __name__ == '__main__':
print getHelloWorld('.', 'test.txt')
print getHelloAndChina('.', 'test.txt')

文本内容如下:

Hello_123
World 456_China

python 读取指定文件信息并拼接的更多相关文章

  1. bat 读取当前目录指定文件信息并拼接

    bat 读取指定文件的信息并拼接成指定格式

  2. python 读取指定文件夹中的指定文件类型的文件名

    import numpy as np import os path = 'F:\\wenjian'#指定文件所在路径 filetype ='.csv'#指定文件类型 def get_filename( ...

  3. Python读取指定文件夹(包括当前目录、子目录、子文件)

    http://blog.csdn.net/lsq2902101015/article/details/51305825

  4. shell 读取目录指定文件并截取拼接

    shell脚本读取指定文件并拼接成指定的版本信息

  5. Python读取SQLite文件数据

    近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...

  6. Python读取xlsx文件

    Python读取xlsx文件 脚本如下: from openpyxl import load_workbook workbook = load_workbook(u'/tmp/test.xlsx') ...

  7. python读取指定字节长度的文本

    软件版本 Python 2.7.13;   Win 10 场景描述 1.使用python读取指定长度的文本: 2.使用python读取某一范围内的文本. Python代码 test.txt文本内包含的 ...

  8. cocos2d-x:读取指定文件夹下的文件名称+解决中文乱码(win32下有效)

    援引:http://blog.csdn.net/zhanghefu/article/details/21284323 http://blog.csdn.net/cxf7394373/article/d ...

  9. 强悍的Python读取大文件的解决方案

    这是一道著名的 Python 面试题,考察的问题是,Python 读取大文件和一般规模的文件时的区别,也即哪些接口不适合读取大文件. 1. read() 接口的问题 f =open(filename, ...

随机推荐

  1. POJ2352 star

    传送门 这道题有个非常好听的名字,求二维偏序! 听起来似乎很高端,但就是让求满足对于每个i,xi < xj && yi < yj的个数. 这道题特别良心,给的顺序都是y递增 ...

  2. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

  3. sqlserver新加一自增长的列,并且更新为行号

    --查询行号 select row_number()over(order by CHECKTIME )as RowNum,*from CHECKINOUT --更新id列为行号 update CHEC ...

  4. JSP页面结构

    1.表达式格式(experssion):<%=value %>//用来在页面中调用java表达式,从而得到返回值 <%=new java.util.Date();%> 2.小脚 ...

  5. Objective-C 继承与类

    创建: 2018/01/20 完成: 2018/01/21 更新: 2018/01/22 标题前增加 [Objective-C]  继承的概念  父类与子类 ●继承: 继承其他类 ●父类: 被继承的类 ...

  6. Thinkphp模板标签if和eq的区别和比较

    在TP模板语言中.if和eq都可以用于变量的比较.总结以下几点: 1.两个变量的比较: <if condition=”$item.group_id eq $one.group_id”> & ...

  7. bzoj 1770: [Usaco2009 Nov]lights 燈【高斯消元+dfs】

    参考:https://blog.csdn.net/qq_34564984/article/details/53843777 可能背了假的板子-- 对于每个灯建立方程:与它相邻的灯的开关次数的异或和为1 ...

  8. less新手入门(四)—— Mixin Guards

    八.Mixin Guards 有条件的 mixin 当您想要匹配表达式时,相对于简单的值或特性,Guards是有用的.如果您熟悉函数式编程,您可能已经遇到过它们. 为了尽可能地保持CSS的声明性质,在 ...

  9. 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  10. [ TJOI 2010 ] 打扫房间

    \(\\\) Description 给出一个\(N\times M\) 的网格,一些格子是污点,求是否能用多个封闭的环路覆盖所有不是污点的格点. 封闭的环路覆盖的含义是,每条路径都必须是一个环,且每 ...