![](/img/trans.png)
[英]How can I parse a string into CSV format in python? Not actually writing to CSV file, can't use csv library
[英]How can I use a CSV file in python?
當我使用在線教程進行時,它一直告訴我引用有錯誤,但編輯后我仍然沒有看到問題。 你能幫忙嗎?
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
將文件名更改為“CSV_birthdays.csv”
然后使用 pandas
import pandas as pd
df = pd.read_csv("CSV_birthdays.csv")
df.head()
df.tail()
df.info()
df.describe()
謝謝你。 我可能不明白 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.