小编pyt*_*der的帖子

如何使用python将列表列表写入excel?

如何使用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 excel python-3.x

8
推荐指数
2
解决办法
5万
查看次数

如何使用python单独替换字符串中的第一个字符?

如何使用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)

python replace python-3.x

5
推荐指数
2
解决办法
2万
查看次数

如何在postgresql中列出最近7天的记录?

SELECT * FROM dummy WHERE entry_date 位于时区“UTC”的 current_date 和 current_date 之间 - 间隔“7 天”;

我想显示当前日期和过去 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)

sql postgresql

5
推荐指数
1
解决办法
6820
查看次数

TypeError:Decimal 类型的对象不可 JSON 序列化

TypeError: Object of type Decimal is not JSON serializable


当我运行时,postman api我收到上述错误,因为sales_qty是十进制,我不知道如何decimalfor循环中解析并将其作为 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 …

python json decimal flask-sqlalchemy flask-restful

2
推荐指数
1
解决办法
9006
查看次数

如何在python3中找到文件的长度?

我的第一个.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 text-files content-length

0
推荐指数
1
解决办法
1万
查看次数