简体   繁体   English

如何使用 Python 将漂亮的 output 转换为 CSV 文件

[英]How to convert the prettytable output to a CSV file using Python

I am trying to convert the table created using the PrettyTable to CSV format using Python in an AWS lambda function. I can able to generate the CSV file but the content inside the CSV file is not in CSV format.我正在尝试使用 AWS lambda function 中的 Python 将使用 PrettyTable 创建的表转换为 CSV 格式。我可以生成 CSV 文件,但 CSV 文件中的内容不在 180105388 格式中。 Could someone help me to fix if there is any issue in the code?如果代码中有任何问题,有人可以帮我解决吗?

import os
import json
from prettytable import PrettyTable
data = PrettyTable(["Col1","Col2","Col3"])

data.add_row(["test1","test2","test3"])
data.add_row(["test4","test5","test6"])
data.add_row(["test7","test8","test9"])
print(data)
data_string = data.get_string()
with open('/tmp/test.csv',w) as f:
    f.write(data_string)
    f.close

The data content inside the csv file is printing in the same way as in the terminal. csv 文件中的数据内容打印方式与终端相同。 Could anyone help me to fix the issue?谁能帮我解决这个问题?

You can use the get_csv_string() function instead to get the data correctly formatted for CSV output.您可以改用get_csv_string()函数来获取为 CSV 输出正确格式化的数据。 This can then be written to an output CSV file:然后可以将其写入输出 CSV 文件:

from prettytable import PrettyTable

data = PrettyTable(["Col1","Col2","Col3"])

data.add_row(["test1","test2","test3"])
data.add_row(["test4","test5","test6"])
data.add_row(["test7","test8","test9"])
print(data)

with open('test.csv', 'w', newline='') as f_output:
    f_output.write(data.get_csv_string())

Giving you test.csv containing:给你test.csv包含:

Col1,Col2,Col3
test1,test2,test3
test4,test5,test6
test7,test8,test9

The get_string() function just returns the data in the same format as would be printed. get_string()函数只返回与打印格式相同的数据。

The output can be converted to csv by importing csv lib通过导入 csv lib 可以将 output 转换为 csv

import os
import json
import csv
from prettytable import PrettyTable

data = PrettyTable(["Col1","Col2","Col3"])

data.add_row(["test1","test2","test3"])
data.add_row(["test4","test5","test6"])
data.add_row(["test7","test8","test9"])
print(data)
data_string = data.get_string()
with open('test.csv', 'w', newline = '') as f:
     writer = csv.writer(f)
     writer.writerows(data)

However this is not recommended since it will distort the format entirely if the output is exported to any format other than the default prettytable format.但是,不建议这样做,因为如果将 output 导出为默认漂亮格式以外的任何格式,它会完全扭曲格式。 It is therefore recommended to use Pandas instead in such case因此,建议在这种情况下使用 Pandas

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Glue 作业将 JSON 从 s3 转换为 CSV 文件并将其保存在同一个 s3 存储桶中 - How to convert JSON to CSV file from s3 and save it in same s3 bucket using Glue job 如何使用 nodejs 将 csv 转换为 AWS Lamda 中的 html 表 - How to convert csv to html table in AWS Lamda using nodejs 如何使用 python 云函数将存储在云存储中的 docx 文件转换为 pdf 并将其存储在那里 - How to convert a docx file that is stored in cloud storage to pdf and store it there as well using python cloud function 如何使用 Python 中的 Pandas 从 s3 存储桶中读取 csv 文件 - How to read a csv file from an s3 bucket using Pandas in Python 如何使用 spark-java 从 GCS 读取 csv 文件? - How to read csv file from GCS using spark-java? csv output 对于 python 中的所有 ec2 实例 - csv output for all ec2 instances in python 从 AWS lambda function 中的 s3 存储桶中读取 .mdb 或 .accdb 文件并使用 python 将其转换为 excel 或 csv - Reading .mdb or .accdb file from s3 bucket in AWS lambda function and converting it into excel or csv using python 如何在 Ubuntu 中使用 PuTTYgen 将 PEM 文件转换为 PPK? - How to convert PEM file to PPK using PuTTYgen in Ubuntu? 如何在 python 中使用 presignedurl 将用户(csv)导入 AWS cognito - How to import users(csv) to AWS cognito using presignedurl in python 如何在没有 pandas 的情况下使用 Python 删除存储在 CSV 中的科学计数法 E? - How to remove the Scientific notation E getting stored in CSV using Python without pandas?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM