我正在尝试在 python 中编写一个 if 语句,该语句将使用以下代码写入 csv 文件。我希望 if 语句写入 CSV 文件中的单元格。我想让它说“X 大于 Y”或“Y 大于 X”。
from bs4 import BeautifulSoup
import csv
x = int(raw_input())
y = int(raw_input())
f = csv.writer(open("pe_ratio.csv", "w"))
f.writerow(["Company Name","PE"])
f.writerow('[x+y,
if x > y:
print "X is bigger than y"
if x < y:
print "Y is bigger than x"
]')
尝试纠正它时,我收到以下错误:
"pe_ratio.py", line 8
f.writerow('[x+y,
SyntaxError: EOL while scanning string literal
这段代码解决了这个问题:
from bs4 import BeautifulSoup
import csv
x = int(raw_input())
y = int(raw_input())
f = csv.writer(open("pe_ratio.csv", "w"))
if x > y:
f.writerow(["X is bigger than y"])
elif x < y:
f.writerow(["Y is bigger than X"])