简体   繁体   English

python csv有问题

[英]having problems with python csv

I'am having trouble with python csv module I'am trying to write a newline in a csv file is there any reson why it would not work?我在使用 python csv 模块时遇到问题我试图在 csv 文件中写一个换行符 是否有任何原因它不起作用?

Code:代码:

csv writing function
    def write_response_csv(name,games,mins):
    with open("sport_team.csv",'w',newline='',encoding='utf-8') as csv_file:
        fieldnames=['Vardas','Žaidimai','Minutės']
        writer = csv.DictWriter(csv_file,fieldnames=fieldnames)
        writer.writeheader()
        writer.writerow({'Vardas':name,'Žaidimai':games,"Minutės":mins})


    with requests.get(url,headers=headers) as page:
    content = soup(page.content,'html.parser')
    content = content.findAll('table',class_='table01 tablesorter')
    names = find_name(content)
    times = 0
    for name in names:
        matches = find_matches(content,times)
        min_in_matches = find_min(content,times)
        times +=1
        csv_file = write_response_csv(name,matches,min_in_matches)
        try:
            print(name,matches,min_in_matches)
        except:
            pass

When you call your write_response_csv function it is reopening the file and starting at line 1 again in the csv file and each new line of data you are passing to that function is overwriting the previous one written.当您调用 write_response_csv 函数时,它会重新打开文件并再次从 csv 文件的第 1 行开始,并且您传递给该函数的每一行新数据都会覆盖之前写入的数据。 What you could do try is creating the csv file outside of the scope of your writer function and setting your writer function to append mode instead of write mode.您可以尝试在编写器函数范围之外创建 csv 文件,并将编写器函数设置为追加模式而不是写入模式。 This will ensure that it will write the data on the next empty csv line, instead of starting at line 1.这将确保它将数据写入下一个空的 csv 行,而不是从第 1 行开始。

#Outside of function scope
fieldnames=['Vardas','Žaidimai','Minutės']
#Create sport_team.csv file w/ headers
with open('sport_team.csv', 'w',encoding='utf-8') as csv_file:
    writer = csv.DictWriter(csv_file, fieldnames)
    writer.writeheader()

#Write response function
def write_response_csv(name,games,mins):
        with open('sport_team.csv','a',encoding='utf-8') as csv_file:
            writer = csv.DictWriter(csv_file, fieldnames)
            writer.writerow({'Vardas':name,'Žaidimai':games,"Minutės":mins})

Note : You will run into the same issue if you are reusing this script to continuously add new lines of data to the same file because each time you run it the code that creates the csv file will essentially recreate a blank sport_team.csv file with the headers.注意:如果您重复使用这个脚本来不断地向同一个文件添加新的数据行,您将遇到同样的问题,因为每次运行它时,创建 csv 文件的代码实际上将重新创建一个空白的 Sport_team.csv 文件标题。 If you would like to reuse the code to continuously add new data, I would look into using os.path and utilizing it to confirm if sport_team.csv exists already and if so, to not run that code after the fieldnames.如果您想重复使用代码来不断添加新数据,我会考虑使用 os.path 并利用它来确认 Sport_team.csv 是否已经存在,如果存在,则不要在字段名称之后运行该代码。

Try using metabob, it find code errors for you.尝试使用 metabob,它会为您查找代码错误。 I've been using it as a Python beginner, and has been pretty successful with it.我一直在使用它作为 Python 初学者,并且已经非常成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM