简体   繁体   English

如何在csv中修复我的Pandas Dataframe输出

[英]How can I fix my Pandas Dataframe Output in csv

When trying to run the following code I am not getting expected results. 尝试运行以下代码时,没有得到预期的结果。 I want the output to span multiple columns. 我希望输出跨越多列。

It looks like it is throwing quotation marks around the data like this 看起来像这样在数据周围加上引号

"column1, column2, column3" “第1栏,第2栏,第3栏”
"sdf,e,sd" “ sdf,e,sd”

import pandas as pd
outputPath = r'C:\Users\Jfairfield\Desktop\output.csv'
testPath = r'C:\Users\Jfairfield\Desktop\test.csv'
csvData = pd.read_csv(testPath, 'Sheet1')
csvData.to_csv(outputPath, index=False)

Input: 输入:

在此处输入图片说明

Input as Text 输入为文字
column1, column2, column3 第1列,第2列,第3列
sdf,e,sd sdf,e,sd

Current Output: 电流输出:

在此处输入图片说明

outputPath = r'C:\Users\xxx\Desktop\Python fo excel\mycsv - Copy.csv'
testPath = r'C:\Users\xxx\Desktop\Python fo excel\mycsv.csv'
csvData = pd.read_csv(testPath,  sep=',', engine='python')
print(csvData)
csvData.to_csv(outputPath, index=False, sep=',')

output 输出

     A  B   C
0  sdf  e  sd

It's likely you have a quoted lines in the text file, which escapes the delimiter while reading data in, you can try set quoting=3 (Quote None) to avoid the behavior: 可能是文本文件中有带引号的行,在读入数据时转义了定界符,您可以尝试设置quoting=3 (Quote None)来避免这种情况:

Example : 范例

stripQuote = lambda x: x.strip('"')
​
df = pd.read_csv(StringIO("""
"a,b,c"
"d,e,f"
"""), quoting=3, converters={0: stripQuote, 2: stripQuote})
​
df.columns = ['a','b','c']

df
#   a   b   c
#0  d   e   f

暂无
暂无

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

相关问题 如何通过python格式化我的pandas数据框,以在csv输出中获取列? - How do I format my pandas dataframe through python to get columns in the csv output? 如何将 pandas dataframe output 保存到新创建的文件夹中的 ZCC8D68C551C4A9A6D5313EDZDE 文件中? - How can I save pandas dataframe output to a CSV file in a newly created folder? 如何让我的数据 (Pandas Dataframe) 在我的 Flet (Python) 应用程序 output 中正确对齐? - How can I get my data (Pandas Dataframe) to align properly in my Flet (Python) app output? 如何在 Pandas DataFrame 中为我的预测结果添加一列然后另存为 CSV? - How can I add a column for my predicted results in a Pandas DataFrame then save as a CSV? 如何编辑数据框的输出? - How can I edit the output of my dataframe? 保存 CSV 文件的问题如何在 python pandas 中解决这个问题? - Issue with saving CSV file how can I fix this in python pandas? 如何修复熊猫 csv 阅读器上的“错误标记数据”? - How can I fix "Error tokenizing data" on pandas csv reader? 如何将我的 output 显示为 Pandas dataframe? - How do I present my output as a Pandas dataframe? 如何使用 Flask 为 Pandas 数据框设置“下载为 csv”按钮? - How can I set a "download as csv" button for a pandas dataframe with Flask? 如何清理此 csv 数据,以便我可以将其读入 pandas dataframe - How to clean this csv data so that I can read it into a pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM