2016-12-07 3 views
0

https://drive.google.com/open?id=0B29hT1HI-pwxMjBPQWFYaWoyalE) しかし、私は3-4種類のコードメソッドを試してみましたが、繰り返し ""行にNULLバイトが含まれています。私は他のスレッドでこれがあなたのcsvの問題だと読んでいますが、これは私の教授が読み込んで格付けするファイルです。私はそれを変更できませんので、このエラーの解決策を探しています。PYTHON:csvファイルの解析と最大値の検索に関する問題

私が言及したように、私はファイルを開くためにいくつかの異なる方法を試しました。ここに私のベスト2があります:

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() 

この結果、NULLバイトエラーになります。追加のmax_pop行は無視してください。ファイルを読み取った後の次のステップは、行Fの最大値を見つけることです。

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

この結果、NULLバイトエラーになります。

誰でも簡単にこの問題を解決できたら、私はそれを高く評価します。 .txtのよう

ファイル:Googleドライブのリンクを介して提供されているすべての「CSV」ファイルのhttps://drive.google.com/open?id=0B29hT1HI-pwxZzhlMGZGVVAzX28

+0

「この参照」とはなんですか? – GreenMatt

+0

ファイルが壊れているようです。あなたがそれを開くと、野生の文字がたくさん出てくる – Anuj

+2

それはバイナリファイルだと思われます。または、Anujが言ったように壊れています。 AFAIK、CSVリーダーはテキストファイルでのみ動作します。教授にあなたがファイルに関する問題について教えてください。 – GreenMatt

答えて

0

をいくつかのXMLモジュールを使用することができる新しい.txtファイルにはよさそうだ、とあなたの関数largestState()は正しい出力を提供します。最後にprintの代わりにreturnとしてください。

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() 
2

まずcsvファイルではありません。そのgzipのed 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 

あなたは

それを解析する
[~/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'} 
関連する問題