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, ...
随机推荐
- bzoj4869
http://www.lydsy.com/JudgeOnline/problem.php?id=4869 终于A了...参考了下dalao的代码... 拓展欧几里得定理,改了几次就不变了,但是用的时候 ...
- 3-3 编程练习:jQuery键盘事件案例
3-3 编程练习 完善下面的代码,在input框中输入内容的时候同样显示在下面的p标签中 <!DOCTYPE html> <html lang="zh-CN"&g ...
- AOP面向方面编程---postsharp
PostSharp是一个用于在.NET平台上实现AOP(Aspect-Oriented Programming,面向方面编程)的框架,现通过简单的示例代码来演示如何使用postsharp. 1.新建一 ...
- .net中RSA加密解密
1.产生密钥: private static void CreateKey() { using (RSACryptoServiceProvider rsa = new RSACryptoService ...
- 6.12---bug
- LN : leetcode 5 Longest Palindromic Substring
lc 5 Longest Palindromic Substring 5 Longest Palindromic Substring Given a string s, find the longes ...
- 往文件内写入内容(java)
新建个工具类,并标记成静态的,方便调用. package util; import java.io.File;import java.io.FileOutputStream; public class ...
- 微信小程序组件解读和分析:三、swiper滑块视图
swiper滑块组件说明: 滑块视图容器,用于展示图片,可以通过用户拖拽和设置自动切换属性控制图片的切换 组件的使用示例的运行效果如下: 下面是WXML代码: [XML] 纯文本查看 复制代码 ? ...
- 329.-io流(字符-练习-复制文本文件二)
//每次读取的字节长度,一般都是1024的倍数 private static final int BUF_SIZE = 1024; public static void main(String[] a ...
- wparam , lparam 传递消息
01.WM_PAINT消息 LOWORD(lParam)是客户区的宽,HIWORD(lParam)是客户区的高 02.滚动条WM_VSCROLL或WM_HSCROLL消息 LOWORD(wParam) ...