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, ...
随机推荐
- Rails 确认params的统一方法
创建: 2017/11/06 Gemfile ### デバッグ出力の整形 gem 'awesome_print', :group => [:development, :test] a ...
- 10.16NOIP模拟赛
/* 我是一个大sb */ #include<iostream> #include<cstdio> #include<cstring> #include<qu ...
- [Swift]经典解题思路:联合查找Union Find
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- jQuery——表单应用(3)
HTML: <!--表单-多行文本框应用-滚动条高度变化--> <!DOCTYPE html> <html> <head> <meta chars ...
- 论tab切换的几种实现方法
tab切换在网页中很常见,故最近总结了4种实现方法. 首先,写出tab的框架,加上最简单的样式,代码如下: <!DOCTYPE html> <html> <head> ...
- Spring.Net学习笔记(0)-错误汇总
1.错误一:ObjectDefinitionStoreException "Spring.Objects.Factory.ObjectDefinitionStoreException&quo ...
- ch1 About thinking skills
When confronted with a problem , we think about it. The issue, of course, is that our efforts may be ...
- poj1923 Fourier's Lines
思路: 记忆化搜索. n条直线的交点方案数 =(n-r)条平行线与r条直线交叉的交点数+r条直线本身的交点方案 =(n-r)*r+r条直线之间本身的交点方案数(0<r<=n) 于是可以枚举 ...
- Python安装笔记
1.教程
- 用DataReader 分页与几种传统的分页方法的比较
对于数据库的分页,目前比较传统的方法是采用分页存储过程,其实用 DataReader 也可以实现分页,不需要写存储过程,实现效率上也比几种比较流行的分页方法要略快. 在开始这个方法之前,让我们先创建一 ...