我在读取/导入一个csv文件到Pandas Dataframe 中时遇到了一些麻烦。导入没有跳过引号中的逗号。
我试过不同的选择quotechar,但没有任何区别
import csv
import pandas
df = pandas.read_csv( 'test_quote.csv', header=None,sep=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL, encoding='ascii', engine='python')
print(df)
code output
$ python3 test_quote.py
0 1 2 3 4 5 6
0 201571 2080 "December 2 2022" "November 1 - November 30 2022" 487.29
1 345741 5377 "December 3 2022" "November 1 - November 30 2022" 729.35
2 995349 3672 "December 2 2022" "November 1 - November 30 2022" 937.33
3 475601 3672 "December 2 2022" "November 1 - November 30 2022" 790.17
4 228548 3672 "December 7 2022" "November 1 - November 30 2022" 682.38
expected output
$ python3 test_quote.py
0 1 2 3 4
0 201571 2080 "December 2, 2022" "November 1 - November 30, 2022" 487.29
1 345741 5377 "December 3, 2022" "November 1 - November 30, 2022" 729.35
2 995349 3672 "December 2 , 2022" "November 1 - November 30 , 2022" 937.33
3 475601 3672 "December 2 , 2022" "November 1 - November 30 , 2022" 790.17
4 228548 3672 "December 7, 2022" "November 1 - November 30, 2022" 682.38
input file = test_quote.csv
201571, 2080, "December 2, 2022", "November 1 - November 30, 2022", 487.29
345741, 5377, "December 3, 2022", "November 1 - November 30, 2022", 729.35
995349, 3672, "December 2 , 2022", "November 1 - November 30 , 2022", 937.33
475601, 3672, "December 2 , 2022", "November 1 - November 30 , 2022", 790.17
228548, 3672, "December 7, 2022", "November 1 - November 30, 2022", 682.38
1条答案
按热度按时间zz2j4svz1#
逗号后面的多余空格导致了这个问题。使用下面的,但是注意你的大多数参数已经是默认值了。
输出: