1、in a file called lines.py
, implement a program that expects exactly one command-line argument, the name (or path) of a Python file, and outputs the number of lines of code in that file, excluding comments and blank lines. If the user does not specify exactly one command-line argument, or if the specified file’s name does not end in .py
, or if the specified file does not exist, the program should instead exit via sys.exit
.
Assume that any line that starts with #
, optionally preceded by whitespace, is a comment. (A docstring should not be considered a comment.) Assume that any line that only contains whitespace is blank.
import sys
try:
if sys.argv[1].endswith(".py"):
count = len(open(sys.argv[1],'r').readlines())
print(count)
else:
sys.exit
except FileNotFoundError:
print("no file")
2、
Students tend to buy pizza by the slice, but Pinocchio’s also has whole pizzas on its menu too, per this CSV file of Sicilian pizzas, sicilian.csv, below:
Sicilian Pizza,Small,Large
Cheese,$25.50,$39.95
1 item,$27.50,$41.95
2 items,$29.50,$43.95
3 items,$31.50,$45.95
Special,$33.50,$47.95
See regular.csv for a CSV file of regular pizzas as well.
Of course, a CSV file isn’t the most customer-friendly format to look at. Prettier might be a table, formatted as ASCII art, like this one:
+------------------+---------+---------+
| Sicilian Pizza | Small | Large |
+==================+=========+=========+
| Cheese | $25.50 | $39.95 |
+------------------+---------+---------+
| 1 item | $27.50 | $41.95 |
+------------------+---------+---------+
| 2 items | $29.50 | $43.95 |
+------------------+---------+---------+
| 3 items | $31.50 | $45.95 |
+------------------+---------+---------+
| Special | $33.50 | $47.95 |
+------------------+---------+---------+
In a file called pizza.py
, implement a program that expects exactly one command-line argument, the name (or path) of a CSV file in Pinocchio’s format, and outputs a table formatted as ASCII art using tabulate
, a package on PyPI at pypi.org/project/tabulate. Format the table using the library’s grid
format. If the user does not specify exactly one command-line argument, or if the specified file’s name does not end in .csv
, or if the specified file does not exist, the program should instead exit via sys.exit
.
tabulate函数
import sys
import csv
from tabulate import tabulate
try:
table = []
with open (sys.argv[1],"r",)as file:
reader = csv.DictReader(file)
for row in reader:
table.append({"Sicilian Pizza": row["Sicilian Pizza"], "Small": row["Small"], "Large": row["Large"]})
print(tabulate(table, headers= "firstrow", tablefmt='grid'))
except FileNotFoundError:
print("no file")
3\
In a file called scourgify.py
, implement a program that:
- Expects the user to provide two command-line arguments:
- the name of an existing CSV file to read as input, whose columns are assumed to be, in order,
name
andhouse
, and - the name of a new CSV to write as output, whose columns should be, in order,
first
,last
, andhouse
.
- the name of an existing CSV file to read as input, whose columns are assumed to be, in order,
- Converts that input to that output, splitting each
name
into afirst
name andlast
name. Assume that each student will have both a first name and last name.
If the user does not provide exactly two command-line arguments, or if the first cannot be read, the program should exit via sys.exit
with an error message.
import sys
import csv
# 输入读取的现有CSV文件的名称,其列按顺序假定为名称和房屋
# 输出写入的新CSV的名称,其列应按顺序为first、last和house。将每个名称拆分为名字和姓氏
if sys.argv().endswith(".csv")
print("invaild")
sys.exit
try:
with open(sys.argv[1], "r")as file:
reader = csv.DictReader(file)
for row in reader:
with open("after.csv", "a")as file:
firstname, lastname = row["name"].split(",")
house = row["house"]
writer = csv.DictWriter(file, fieldnames = ["firstname", "lastname", "house"])
writer.writeheader()
writer.writerow({"firstname": firstname, "lastname": lastname, "house": house})
except IndexError:
print("Too few command-line arguments")
4、
In a file called shirt.py
, implement a program that expects exactly two command-line arguments:
- in
sys.argv[1]
, the name (or path) of a JPEG or PNG to read (i.e., open) as input - in
sys.argv[2]
, the name (or path) of a JPEG or PNG to write (i.e., save) as output
The program should then overlay shirt.png (which has a transparent background) on the input after resizing and cropping the input to be the same size, saving the result as its output.
Open the input with Image.open
, per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.open, resize and crop the input with ImageOps.fit
, per pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.fit, using default values for method
, bleed
, and centering
, overlay the shirt with Image.paste
, per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.paste, and save the result with Image.save
, per pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.save.
The program should instead exit via sys.exit
:
- if the user does not specify exactly two command-line arguments,
- if the input’s and output’s names do not end in
.jpg
,.jpeg
, or.png
, case-insensitively, - if the input’s name does not have the same extension as the output’s name, or
- if the specified input does not exist.
Assume that the input will be a photo of someone posing in just the right way, like these demos, so that, when they’re resized and cropped, the shirt appears to fit perfectly.