python 读取指定文件信息并拼接
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 读取指定文件信息并拼接的更多相关文章
- bat 读取当前目录指定文件信息并拼接
bat 读取指定文件的信息并拼接成指定格式
- python 读取指定文件夹中的指定文件类型的文件名
import numpy as np import os path = 'F:\\wenjian'#指定文件所在路径 filetype ='.csv'#指定文件类型 def get_filename( ...
- Python读取指定文件夹(包括当前目录、子目录、子文件)
http://blog.csdn.net/lsq2902101015/article/details/51305825
- shell 读取目录指定文件并截取拼接
shell脚本读取指定文件并拼接成指定的版本信息
- Python读取SQLite文件数据
近日在做项目时,意外听说有一种SQLite的数据库,相比自己之前使用的SQL Service甚是轻便,在对数据完整性.并发性要求不高的场景下可以尝试! 1.SQLite简介: SQLite是一个进程内 ...
- Python读取xlsx文件
Python读取xlsx文件 脚本如下: from openpyxl import load_workbook workbook = load_workbook(u'/tmp/test.xlsx') ...
- python读取指定字节长度的文本
软件版本 Python 2.7.13; Win 10 场景描述 1.使用python读取指定长度的文本: 2.使用python读取某一范围内的文本. Python代码 test.txt文本内包含的 ...
- cocos2d-x:读取指定文件夹下的文件名称+解决中文乱码(win32下有效)
援引:http://blog.csdn.net/zhanghefu/article/details/21284323 http://blog.csdn.net/cxf7394373/article/d ...
- 强悍的Python读取大文件的解决方案
这是一道著名的 Python 面试题,考察的问题是,Python 读取大文件和一般规模的文件时的区别,也即哪些接口不适合读取大文件. 1. read() 接口的问题 f =open(filename, ...
随机推荐
- TI BLE: Advertisement
#define GAPROLE_ADVERT_ENABLED 0x305 //!< Enable/Disable Advertising. Read/Write. Size is uint8. ...
- 牛客OI周赛2-提高组
A.游戏 链接:https://www.nowcoder.com/acm/contest/210/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语 ...
- (数论)51NOD 1135 原根
设m是正整数,a是整数,若a模m的阶等于φ(m),则称a为模m的一个原根.(其中φ(m)表示m的欧拉函数) 给出1个质数P,找出P最小的原根. Input 输入1个质数P(3 <= P &l ...
- react key的作用
react中的key属性,它是一个特殊的属性,它是出现不是给开发者用的(例如你为一个组件设置key之后不能获取组件的这个key props),而是给react自己用的. 简单来说,react利用key ...
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...
- [LOJ#10132]异象石
Description Adera 是 Microsoft 应用商店中的一款解谜游戏. 异象石是进入 Adera 中异时空的引导物,在 Adera 的异时空中有一张地图.这张地图上 有 N 个点,有 ...
- 数据传递-------@PathVariable
package com.wh.handler; /** * 通过@PathVariable可以绑定占位符参数到方法参数中,例如 * @PathVariable("userId") ...
- 403 Frog Jump 青蛙过河
一只青蛙想要过河. 假定河流被等分为 x 个单元格,并且在每一个单元格内都有可能放有一石子(也有可能没有). 青蛙可以跳上石头,但是不可以跳入水中.给定石子的位置列表(用单元格序号升序表示), 请判定 ...
- JSP/Servlet Web应用中.properties文件的放置与读取
本地项目 在本地类库中,我经常使用当前目录来放置.properties文件,这时调用方只要引用我的jar,并且将我的.properties放在他的classpath里面即可,比如: p.load(ne ...
- 386 Lexicographical Numbers 字典序排数
给定一个整数 n, 返回从 1 到 n 的字典顺序.例如,给定 n =1 3,返回 [1,10,11,12,13,2,3,4,5,6,7,8,9] .请尽可能的优化算法的时间复杂度和空间复杂度. 输入 ...