【发布时间】:2018-11-27 14:57:21
【问题描述】:
我是 python 的新手,我已经能够解析我的 xml 文件并得到我所期望的,这是我在 shell 上得到的一些输出:
* p84182 [Goalkeeper] 16 - total_through_ball = [1]
* p84182 [Goalkeeper] 16 - duel_lost = [4]
* p84182 [Goalkeeper] 16 - blocked_scoring_att = [1]
* p84182 [Goalkeeper] 16 - leftside_pass = [46]
* p84182 [Goalkeeper] 16 - dispossessed = [1]
* p84182 [Goalkeeper] 16 - accurate_cross = [2]
* p84182 [Goalkeeper] 16 - att_rf_total = [1]
我想将其转换为包含这 5 列的 csv 文件,但每次运行代码时都会遇到相同的错误:
Traceback (most recent call last):
File "C:\Users\Hp\Documents\test4.py", line 20, in <module>
print.fichier(ID,Position,Shirt,Types,Reponse)
AttributeError: 'builtin_function_or_method' object has no attribute 'fichier'
这是我的python代码:
import os
from xml.etree import ElementTree
import csv
file_name="C:/Users/Hp/Desktop/BYG/PSG-Amiens.xml"
full_file=os.path.abspath(os.path.join('BYG',file_name))
dom=ElementTree.parse(full_file)
fichier=open('data.csv','w')
Stats=dom.findall('SoccerDocument/MatchData/TeamData/PlayerLineUp/MatchPlayer')
Type=dom.findall('SoccerDocument/MatchData/TeamData/PlayerLineUp/MatchPlayer[@PlayerRef="p15780"]/Stat')
for s in Stats:
ID=s.get('PlayerRef')
Position=s.get('Position')
Shirt=s.get('ShirtNumber')
for t in Type:
Types=t.get('Type')
Reponse=t.text
print.fichier(ID,Position,Shirt,Types,Reponse)
fichier.close()
如何解决这个问题?并将我的 xml 解析转换为 csvfile?
【问题讨论】:
-
是不是 : print.fichier 抛出了你的错误?
-
这就是为什么您总是必须在您的问题中提供整个回溯。它提供了更多信息,而不仅仅是文字错误文本。
-
@MatinaG 感谢您的回答,我更新了我的问题,是的,是 print.fichier 引发了您的错误