简体   繁体   中英

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?

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. 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. This will ensure that it will write the data on the next empty csv line, instead of starting at line 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. 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.

Try using metabob, it find code errors for you. I've been using it as a Python beginner, and has been pretty successful with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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