[英]how can I get the values of each line using fileinput.input in python
I have this csv:我有这个 csv:
mycsv.csv我的csv文件
name,last name
yeison, smith
lola, boa
elmo, spitia
anderson, exneider
juan, ortega
I want to show the value of each column.我想显示每列的值。 something like:
就像是:
print (line["word"])
print (line[0])
import fileinput
with fileinput.input('mycsv.csv', inplace=True) as f:
for line in f:
#if f.lineno() == 2:
if line["name"] == "yeison":
print('german, ezequiel')
else:
print(line, end='')
you can use this simple code to read your cvs file (pandas)你可以使用这个简单的代码来读取你的 cvs 文件(pandas)
# Load the Pandas libraries with alias 'pd'
import pandas as pd
# Read data from file 'filename.csv'
# (in the same directory that your python process is based)
# Control delimiters, rows, column names with read_csv (see later)
data = pd.read_csv("filename.csv")
# Preview the first 5 lines of the loaded data
data.head()
If your data isn't large you can just open and parse your file line by line as such:如果您的数据不大,您可以像这样逐行打开和解析您的文件:
with open("mycsv.csv") as f:
lines = [line.strip('\n') for line in f]
for ln in lines:
abc = ln.split(',')
if(len(abc) > 1):
print(abc[0],abc[1])
Otherwise you could use the csv
module to do this effectively否则,您可以使用
csv
模块有效地执行此操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.