Как я могу использовать файл CSV в python? - PullRequest
0 голосов
/ 28 апреля 2020

Когда я делал это с помощью онлайн-учебника, он постоянно говорил мне, что с цитатами произошла ошибка, но после редактирования я все еще не вижу проблемы. Можете ли вы помочь?

import csv
with open("CSV_birthdays.txt") as csv_file:
    csv_converter = csv.reader(csv_file, delimiter=',') # This suggests that commas seperate classes
    linenum = 0
    for row in csv_converter:
        if linenum == 0:
            print("There are 4 family members I have dealt with so far")
            linenum +=1 # go to the next line
        else: # for the actual columns printing out my sentences
            print(f"\t{row[0]} is {row[1]} years old, and they were born on the {row[2]}th) of {row[3]}{row[4]}.")
            linenum += 1 # print the next line
    print(f"I have the birthdays of {linenum} people")

Это CSV-файл, который я имею в виду

name, age, birthday ,birthday month, birth year
me, 15, 17, March, 2005
my sister, 13 , 1 , 8, 3006
Manson, 10, 22, 11, 2009
Fred, 15, 7, 6, 2004

Ответы [ 2 ]

0 голосов
/ 28 апреля 2020

Спасибо. Возможно, я не понял, что такое pandas, но вы помогли мне понять мои ошибки.

import csv
with open("birthday.txt") as csv_file:
    csv_converter = csv.reader(csv_file, delimiter=',') # This suggests that commas seperate classes
    linenum = 0
    for row in csv_converter:
        if linenum == 0:
            print("There are 4 family members I have dealt with so far")
            linenum +=1 # go to the next line
        else: # for the actual columns printing out my sentences
            name = ''.join(row[0])
            age = ''.join(row[1])
            birthday =''.join(row[2])
            birthmonth =''.join(row[3])
            birthyear = ''.join(row[4])

            print (name +" is" + age +" years old, and they were born on the " + birthday + "of " + birthmonth + "" + birthyear +".")
            linenum += 1 # print the next line
        print("\there is the original format")
        print ','.join(row)
0 голосов
/ 28 апреля 2020

Измените имя файла на «CSV_birthdays.csv»

Затем используйте pandas

import pandas as pd

df = pd.read_csv("CSV_birthdays.csv")
df.head()
df.tail()
df.info()
df.describe()
...