Py学习  »  Python

如何修复CSV文件(python 3)中的“,”分隔

ArthuroTech • 3 年前 • 1501 次点击  

该计划是采取“千分之三”,并在错误的地方分离

第二栏应该是:财富中值:“227891”

第三栏:平均财富:“564653”

第四栏:人口:“6866”

最后,程序收到一个错误,因为总数超过了列数

我的代码:

import matplotlib.pyplot as plt

separate = ','

with open('wealth-per-country.csv', 'r') as arq:
    for line_number, content in enumerate(arq):
        if line_number:  
            column = content.strip().split(separate)
            print(f"Country: {column[0]}, \nMedian_Wealth: {column[1]}, \nMean_Wealth: {column[2]}, \nPopulation: {column[3]}")

印刷:

Country: Switzerland, 
Median_Wealth: "227, 
Mean_Wealth: 891", 
Population: "564
Country: Australia, 
Median_Wealth: "181, 
Mean_Wealth: 361", 
Population: "386
Country: Iceland, 
Median_Wealth: "165, 
Mean_Wealth: 961", 
Population: "380
Country: Hong Kong, 
Median_Wealth: "146, 
Mean_Wealth: 887", 
Population: "489
Country: Luxembourg, 
Median_Wealth: "139, 
Mean_Wealth: 789", 
Population: "358
Country: Belgium, 
Median_Wealth: "117, 
Mean_Wealth: 093", 
Population: "246
Country: New Zealand, 
Median_Wealth: "116, 
Mean_Wealth: 433", 
Population: "304
Country: Japan, 
Median_Wealth: "110, 
Mean_Wealth: 408", 
Population: "238
Country: Canada, 
Median_Wealth: "107, 
Mean_Wealth: 004", 
Population: "294
Country: Ireland, 
Median_Wealth: "104, 
Mean_Wealth: 842", 
Population: "272
Traceback (most recent call last):
  File "main.py", line 9, in <module>
    print(f"Country: {column[0]}, \nMedian_Wealth: {column[1]}, \nMean_Wealth: {column[2]}, \nPopulation: {column[3]}")
IndexError: list index out of range

原始CSV数据

Country,Median_Wealth,Mean_Wealth,Population
Switzerland,"227,891","564,653","6,866"
Australia,"181,361","386,058","18,655"
Iceland,"165,961","380,868",250
Hong Kong,"146,887","489,258","6,267"
Luxembourg,"139,789","358,003",461
Belgium,"117,093","246,135","8,913"
New Zealand,"116,433","304,124","3,525"
Japan,"110,408","238,104","104,963"
Canada,"107,004","294,255","29,136"
Ireland,"104,842","272,310","3,491"
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129766
 
1501 次点击  
文章 [ 1 ]  |  最新文章 3 年前