如何使用python 3将列表列表写入excel?
new_list = [["first", "second"], ["third", "fourth"]]
with open("file_name.csv", 'w') as f:
fc = csv.writer(f, lineterminator='\n')
fc.writerows(new_list)
Run Code Online (Sandbox Code Playgroud)
我可以将此列表写入csv文件,但我想如何写入 excel 文件?
如何使用python单独替换字符串中的第一个字符?
string = "11234"
translation_table = str.maketrans({'1': 'I'})
output= (string.translate(translation_table))
print(output)
Run Code Online (Sandbox Code Playgroud)
预期输出:
I1234
Run Code Online (Sandbox Code Playgroud)
实际输出:
11234
Run Code Online (Sandbox Code Playgroud) 我想显示当前日期和过去 7 天。该查询对我不起作用。
SELECT * FROM dummy
WHERE entry_date between current_date and current_date at time zone 'UTC' - interval '7 days
Run Code Online (Sandbox Code Playgroud) TypeError: Object of type Decimal is not JSON serializable当我运行时,postman api我收到上述错误,因为sales_qty是十进制,我不知道如何decimal在for循环中解析并将其作为 json 返回
from flask import Flask, jsonify
import decimal,json
result= [('V_M_001', 'KITE', 'Napkin', 1, 2, 12, 0, Decimal('0'), Decimal('0'), Decimal('0'), Decimal('0')),
('V_M_001', 'KITE', 'Napkin', 2, 4, 34, 5, Decimal('1'), Decimal('4'), Decimal('0'), Decimal('0'))]
def fun():
for i in result:
data_all.append({
"machine_name":i.machine_name,
"location":i.location,
"item_name":i.item_name,
"row_no":i.row_no,
"require_minimum_stk_qty":i.require_minimum_stk_qty,
"capacity":i.capacity,
"stock_qty":i.stock_qty,
"sales_qty":i.sales_qty,
"available_qty":i.available_qty,
"sales_day_qty":i.sales_day_qty,
"sales_week_qty":i.sales_week_qty
})
return jsonify(data_all)
fun()
Run Code Online (Sandbox Code Playgroud)
输出:
TypeError: Object of type Decimal is …
我的第一个.py:
def create_file(file_name):
list=["ab","cd","ef"]
for i in list:
with open(file_name, "a+") as input_file:
print(" {}".format(i), file = input_file)
Run Code Online (Sandbox Code Playgroud)
我的第二个.py:
from first import create_file
def read_file(file_name):
# Create file with content
create_file(file_name)
# Now read file content
input_file = open(file_name, 'r')
for text_line in input_file:
for line in range(len(input_file)):
if "cd" in text_line :
word = (input_file[line + 1])
print(word)
read_file('ss.txt')
Run Code Online (Sandbox Code Playgroud)
我无法找到input_file.
我不知道为什么。有人能帮帮我吗?
预期输出:
ef
然后,如果第 num = 2 行,我希望输出为“ef”。
python ×4
python-3.x ×2
decimal ×1
excel ×1
json ×1
postgresql ×1
replace ×1
sql ×1
text-files ×1