[英]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.