나는과 같이 그 안에 값을 텍스트 파일이 있습니다 읽기 텍스트 파일 값 - 파이썬
1913 - 5,588,048
1914 - 4,202,179
1915 - 1,172,258
1916 - 2,481,675
1917 - 5,521,373
1918 - 6,052,289
1919 - 7,835,400
1920 - 1929
1920 - 10,649,851
1921 - 2,582,495
1922 - 4,763,186
내가 숫자의 두 컬럼의 2D 플롯을합니다. 19xx 및 x 축에, 다른 열은 y 축에 있습니다. split()을 사용하여 '-'을 없애고 ''을 제거하는 데 문제가 있습니다. 어떤 이유로 든 나타납니다. 지금까지
내 코드 :
: PLT X의x = []
y = []
with open('Nickels.txt') as file:
for line in file:
d1 = line.strip().split('-') # Splits X and Y constituents
if len(d1) !=2: continue # Skips the empty lines
d2 = d1[1].strip().split(',') # Splits Y columns
if len(d2) !=3: continue # Skips range lines
x.append(int(d1[0].strip())) # Builds X
y.append(int(''.join(d2))) # Builds Y
내용으로 NP 수입 matplotlib.pyplot로
수입 NumPy와 :이 코드는 문제를 해결해야
import numpy as np
import matplotlib.pyplot as plt
with open("Nickels.txt") as f:
data = f.read()
data = data.split('\n')
data2 = [row.split(' - ',1) for row in data] # to get rid of ' - '
data2[:] = [item for item in x if item != ''] # to get rid of ''
x = []
y = []
for i in range(len(x)): #making lists
x.append(0)
y.append(0)
for j in enumerate(data2):
x[i] = data2[:1]
print(x1)
#will proceed after part above works.
#fig = plt.figure()
#ax1.plot(x1,y1, c='r', label='Data')
#plt.show()
당신이 정확하게 당신이 x 축과 y 축에 대해 동일한 함께하고 싶은 어떤 상태시겠습니까? 또한 최소한 10 줄의 데이터 파일을 제공하십시오 (각 줄은 4 칸 들여 쓰기). –
입력 섹션을 편집했습니다. 파일이 편집 된 파일인지 여부를 실제로 알 수 있습니까? 아니면 실제로 빈 줄이 포함되어 있습니까? –
여기 저기에 몇 개의 빈 줄이 있습니다. 항상 숫자가 반복되기 전에. 예 : 1920 – Michael