> 백엔드 개발 > 파이썬 튜토리얼 > 일 - CSV 파일, ASCII, 문자열 방법

일 - CSV 파일, ASCII, 문자열 방법

Patricia Arquette
풀어 주다: 2024-12-21 07:17:11
원래의
452명이 탐색했습니다.

Day - CSV file, ASCII, String methods

CSV(쉼표로 구분된 값):

CSV 파일은 행을 나타내며 행 내의 각 값은 쉼표로 구분됩니다.
CSV 파일은 Excel과 비슷해 보이지만 Excel 파일은 Excel 소프트웨어에서만 열립니다.
CSV 파일은 모든 운영체제에서 사용됩니다.

CSV 파일은 다음 두 가지 형식으로 열 수 있습니다.

f =open("sample.txt", "r")

with open("sample.txt",’r’) as f:

로그인 후 복사

읽어보기
읽기 위해 파일을 엽니다. 파일이 존재해야 합니다.
와-쓰세요
쓰기 위해 파일을 엽니다. 새 파일을 만들거나 기존 파일을 덮어씁니다.
rb-읽기 바이너리
이는 이미지, 비디오, 오디오 파일, PDF 또는 텍스트가 아닌 파일과 같은 바이너리 파일을 읽는 데 사용됩니다.

store.csv

Player,Score
Virat,80
Rohit,90
Dhoni,100
로그인 후 복사
import csv
f =open("score.csv", "r")
csv_reader = csv.reader(f)
for row in csv_reader:
    print(row)
f.close()

로그인 후 복사
['Player', 'Score']
['Virat', '80']
['Rohit', '90']
['Dhoni', '100']
로그인 후 복사

ASCII:
ASCII는 American Standard Code for Information Interchange를 의미합니다.

ASCII 테이블:
48-57 - 숫자(숫자 0~9)
65-90 - A-Z(대문자)
97-122 - a-z(소문자)

ASCII 테이블을 사용한 패턴 프로그램:

for row in range(5):
    for col in range(row+1):
        print(chr(col+65), end=' ')
    print()
로그인 후 복사
A 
A B 
A B C 
A B C D 
A B C D E 
로그인 후 복사
for row in range(5):
    for col in range(5-row):
        print(chr(row+65), end=' ')
    print()
로그인 후 복사
A A A A A 
B B B B 
C C C 
D D 
E 
로그인 후 복사

for 루프 사용:

name = 'pritha'
for letter in name:
    print(letter,end=' ')

로그인 후 복사
P r i t h a
로그인 후 복사
로그인 후 복사

while 루프 사용:

name = 'pritha'
i=0
while i<len(name):
    print(name[i],end=' ')
    i+=1
로그인 후 복사
P r i t h a
로그인 후 복사
로그인 후 복사

문자열 메서드:
1. 대문자로 표시()
Python의 capitalize() 메서드는 문자열의 첫 번째 문자를 대문자로 변환하고 다른 모든 문자는 소문자로 만드는 데 사용됩니다.

txt = "hello, and welcome to my world."
x = txt.capitalize()
print (x)

로그인 후 복사
Hello, and welcome to my world.
로그인 후 복사
로그인 후 복사

ASCII 테이블을 사용하여 대문자 프로그램 작성:

txt = "hello, and welcome to my world."

first = txt[0]
first = ord(first)-32
first = chr(first)

print(f'{first}{txt[1:]}')
로그인 후 복사
Hello, and welcome to my world.
로그인 후 복사
로그인 후 복사

2.casefold()
Python의 casefold() 메서드는 문자열을 소문자로 변환하는 데 사용됩니다.

txt = "Hello, And Welcome To My World!"
x = txt.casefold()
print(x)
로그인 후 복사
hello, and welcome to my world!
로그인 후 복사
로그인 후 복사

ASCII 테이블을 사용하여 케이스폴더 프로그램 작성:

txt = "Hello, And Welcome To My World!"
for letter in txt:
    if letter>='A' and letter<'Z':
        letter = ord(letter)+32
        letter = chr(letter)
    print(letter,end='')

로그인 후 복사
hello, and welcome to my world!
로그인 후 복사
로그인 후 복사

3.count()
Python의 count() 메소드는 문자열 내에서 하위 문자열의 발생 횟수를 계산하는 데 사용됩니다.

txt = "I love apples, apple is my favorite fruit"
x = txt.count("apple")
print(x)

로그인 후 복사
2
로그인 후 복사
로그인 후 복사

주어진 키에 대한 카운트 프로그램 작성:

txt = "I love apples, apple is my favorite fruit"
key="apple"
l=len(key)
count=0
start=0
end=l
while end<len(txt):
    if txt[start:end]==key:
        count+=1
    start+=1
    end+=1
else:
    print(count)
로그인 후 복사
2
로그인 후 복사
로그인 후 복사

주어진 키가 처음 나타나는 경우 프로그램 작성:

txt = "I love apples, apple is my favorite fruit"
key="apple"
l=len(key)
start=0
end=l
while end<len(txt):
    if txt[start:end]==key:
        print(start)
        break
    start+=1
    end+=1
로그인 후 복사
7
로그인 후 복사

주어진 키가 마지막으로 발생하도록 프로그램 작성:

txt = "I love apples, apple is my favorite fruit"
key="apple"
l=len(key)
start=0
end=l
final=0
while end<len(txt):
    if txt[start:end]==key:
        final=start
    start+=1
    end+=1
else:
    print(final)
로그인 후 복사
15
로그인 후 복사

작업:

for row in range(4):
    for col in range(7-(row*2)):
        print((col+1),end=" ") 
    print()

로그인 후 복사
1 2 3 4 5 6 7 
1 2 3 4 5 
1 2 3 
1 
로그인 후 복사
for row in range(5):
    for col in range(5-row):
        print((row+1)+(col*2),end=" ") 
    print()
로그인 후 복사
1 3 5 7 9 
2 4 6 8 
3 5 7 
4 6 
5 
로그인 후 복사

위 내용은 일 - CSV 파일, ASCII, 문자열 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿