PYTHON:解析csv文件和查找最大值的问题

2024-05-17 11:34:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要读入文件https://drive.google.com/open?id=0B29hT1HI-pwxMjBPQWFYaWoyalE) 但是,我尝试了3-4种不同的代码方法,并反复出现错误:“line contains NULL byte”。我读到其他线程,这是一个问题与您的csv,但这是我的教授将加载和分级我的文件,我不能修改它,所以我正在寻找一个解决方案围绕这个错误。你知道吗

正如我提到的,我尝试了几种不同的方法来打开这个文件。这是我最好的两个:

def largestState(): 
    INPUT  = "statepopulations.csv"
    COLUMN = 5   # 6th column

    with open(INPUT, "rU") as csvFile:
        theFile  = csv.reader(csvFile)
        header = next(theFile, None)    # skip header row
        pop = [float(row[COLUMN]) for row in theFile]

    max_pop = max(pop)
    print max_pop

largestState()

这将导致空字节错误。请忽略额外的max\u pop行。读取中的文件后的下一步是查找F行的最大值

def test():
with open('state-populations.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row
test()

这将导致空字节错误。你知道吗

如果有人能为这个问题提供一个简单的解决办法,我将不胜感激。你知道吗

文件格式为.txt:https://drive.google.com/open?id=0B29hT1HI-pwxZzhlMGZGVVAzX28


Tags: 文件csv方法httpscomid错误google
2条回答

新的.txt文件看起来不错,函数largestState()给出了正确的输出。最后用return代替print。你知道吗

def largestState(): 
    INPUT  = "state-populations.txt"
    COLUMN = 5   # 6th column

    with open(INPUT, "rU") as csvFile:
        theFile  = csv.reader(csvFile)
        header = next(theFile, None)    # skip header row
        pop = [float(row[COLUMN]) for row in theFile]

    max_pop = max(pop)
    return(max_pop)

largestState()

首先,您通过Google Drive链接提供的“csv”文件不是csv文件。它是一个gzip的xml文件。你知道吗

[~/Downloads] file state-populations.csv
state-populations.csv: gzip compressed data, from Unix

[~/Downloads] gzip -d state-populations.csv
gzip: state-populations.csv: unknown suffix   ignored

[~/Downloads] mv state-populations.csv state-populations.csv.gz

[~/Downloads] gzip -d state-populations.csv.gz

[~/Downloads] ls state-populations.csv
state-populations.csv
[~/Downloads] file state-populations.csv
state-populations.csv: XML 1.0 document text, ASCII text, with very long lines

您可以使用一些xml模块来解析它

[~/Downloads] python
Python 2.7.10 (default, Jul 30 2016, 18:31:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import xml
>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('state-populations.csv')
>>> root = tree.getroot()
>>> root
<Element '{http://www.gnumeric.org/v10.dtd}Workbook' at 0x10ded51d0>
>>> root.tag
'{http://www.gnumeric.org/v10.dtd}Workbook'
>>> for child in root:
...     print child.tag, child.attrib
...
{http://www.gnumeric.org/v10.dtd}Version {'Epoch': '1', 'Full': '1.12.9', 'Major': '12', 'Minor': '9'}
{http://www.gnumeric.org/v10.dtd}Attributes {}
{urn:oasis:names:tc:opendocument:xmlns:office:1.0}document-meta {'{urn:oasis:names:tc:opendocument:xmlns:office:1.0}version': '1.2'}
{http://www.gnumeric.org/v10.dtd}Calculation {'ManualRecalc': '0', 'MaxIterations': '100', 'EnableIteration': '1', 'IterationTolerance': '0.001', 'FloatRadix': '2', 'FloatDigits': '53'}
{http://www.gnumeric.org/v10.dtd}SheetNameIndex {}
{http://www.gnumeric.org/v10.dtd}Geometry {'Width': '864', 'Height': '322'}
{http://www.gnumeric.org/v10.dtd}Sheets {}
{http://www.gnumeric.org/v10.dtd}UIData {'SelectedTab': '0'}

相关问题 更多 >