【笨办法学python 进阶版】【ex08_cut】笨办法实现--cut部分命令

1、

今天做了第八课的习题

实现了cut命令部分功能
实现命令入下:

test_cut.py 456.txt  -f 3,5 -d ":"
test_cut.py 123.txt  -f 3,5 -d " "

注意win环境下 -d后面的参数需要双引号,通过isspace()判断可以忽略空格个数,win环境输入-d ’ '会报error: unrecognized arguments: ’ 的错误, 但是在Linux环境下就可以单引号输入参数。
2、test_cut.py 如下:

import argparse

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("filename")
    parser.add_argument("-d", "--delimiter",  type=str)
    parser.add_argument("-f", "--fields",  nargs='+')
    return parser.parse_args()

def cut_func(args):
    with open(args.filename, 'r', encoding='utf-8') as f:
        for line in f.readlines():
            for field in args.fields[0].split(','):
                if args.delimiter.isspace():
                    print(line.split()[int(field)-1], end="\t")
                else:
                    print(line.split(args.delimiter)[int(field)-1], end="\t")
            print(end='\n')
            
args = parse_args()
cut_func(args)

说明:

1、运行环境python3.7 pycharm;
2、https://github.com/zedshaw/learn-more-python-the-hard-way-solutions 有原作者的code;
3、遇到的问题:
parse_args()里面:
nargs=’+’,至少要有一个f的参数值,nargs=’*’,f的参数值可以为0
nargs=num返回的是一个长度为1的list比如[‘3,4’],所有的参数值都放在一起了
type=list, 返回的是这样的[[‘3’, ‘,’, ‘4’, ‘,’]]结果
4、cut截取数据是从1开始;
5、作者zed只用了5分钟,完成的命令入下:

python3.6  cut.py " " 10

但是他是处理input的数据,再拆分的,而我是去直接拆分一个"123.txt"这样的文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python进阶书籍,Transform Your Ideas into High-Quality Python Code! Zed Shaw has perfected the world’s best system for becoming a truly effective Python 3.x developer. Follow it and you will succeed—just like the tens of millions of programmers he’s already taught. You bring the discipline, commitment, and persistence; the author supplies everything else. In Learn Python 3 the Hard Way, Zed Shaw taught you the basics of Programming with Python 3. Now, in Learn More Python 3 the Hard Way, you’ll go far beyond the basics by working through 52 brilliantly crafted projects. Each one helps you build a key practical skill, combining demos to get you started and challenges to deepen your understanding. Zed then teaches you even more in 12 hours of online videos, where he shows you how to break, fix, and debug your code. First, you’ll discover how to analyze a concept, idea, or problem to implement in software. Then, step by step, you’ll learn to design solutions based on your analyses and implement them as simply and elegantly as possible. Throughout, Shaw stresses process so you can get started and build momentum, creativity to solve new problems, and quality so you’ll build code people can rely on. Manage complex projects with a programmer’s text editor Leverage the immense power of data structures Apply algorithms to process your data structures Master indispensable text parsing and processing techniques Use SQL to efficiently and logically model stored data Learn powerful command-line tools and skills Combine multiple practices in complete projects It’ll be hard at first. But soon, you’ll just get it—and that will feel great! This course will reward you for every minute you put into it. Soon, you’ll go beyond merely writing code that runs: you’ll craft high-quality Python code that solves real problems. You’ll be a serious Python programmer. Perfect for Everyone Who’s Already Started Working with Python, including Junior Developers and Seasoned Python Pr

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值