![](/img/trans.png)
[英]Python graph from txt(csv) file and there are 5 columns, How Can I pick 3rd or 4th columns for y axis?
[英]Python, Adding 3rd and 4th rows of a CSV file together
Python 的全新用戶,無論如何都不是程序員,很抱歉這個新手問題。
我有一個 CSV 文件,我需要為每一列添加第 3 行和第 4 行,最好的選擇是將結果 output 到一個新文件中。
我意識到這不是一個難題,但是第一次跳入這個問題有點不知所措。
我已經搜索了一個示例程序來移植,但沒有任何結果。
非常感謝您提供的任何幫助。
這里:
以此為例。csv:
column1,column2,column3
1,2,3
4,5,6
7,8,9
8,7,6
CSV_filename = 'example.csv'
total_columns = 3
results = []
import csv
with open(CSV_filename) as csv_file:
temp = []
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
line_count += 1
elif line_count == 3:
for i in range(total_columns):
temp.append(row[i])
line_count += 1
elif line_count == 4:
for i in range(total_columns):
temp_eq = temp[i]
final_prep = int(temp_eq) + int(row[i])
results.append(final_prep)
line_count += 1
else:
line_count += 1
print(results)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.