在Python中从stdin阅读CSV文件并修改

44u64gxh  于 2023-02-14  发布在  Python
关注(0)|答案(4)|浏览(170)

我需要从标准输入中读取csv文件,并只输出值等于列中指定值的行。我的输入如下:

2
 Kashiwa
 Name,Campus,LabName
 Shinichi MORISHITA,Kashiwa,Laboratory of Omics
 Kenta Naai,Shirogane,Laboratory of Functional Analysis in Silico
 Kiyoshi ASAI,Kashiwa,Laboratory of Genome Informatics
 Yukihide Tomari,Yayoi,Laboratory of RNA Function

我的输出应该是这样的:

Name,Campus,LabName
 Shinichi MORISHITA,Kashiwa,Laboratory of Omics
 Kiyoshi ASAI,Kashiwa,Laboratory of Genome Informatics

我需要找出column#2中的值==柏和的人,并且不在stdout中输出stdin的前2行。
到目前为止,我只是尝试从stdin读取到csv,但我得到的每一行都是字符串列表(正如csv文档中所期望的)。我可以改变这一点吗?

#!usr/bin/env python3

 import sys
 import csv

 data = sys.stdin.readlines()

 for line in csv.reader(data):

      print(line)

输出:

['2']
 ['Kashiwa']
 ['Name', 'Campus', 'LabName']
 ['Shinichi MORISHITA', 'Kashiwa', 'Laboratory of Omics']
 ['Kenta Naai', 'Shirogane', 'Laboratory of Functional Analysis in 
 Silico']
 ['Kiyoshi ASAI', 'Kashiwa', 'Laboratory of Genome Informatics']
 ['Yukihide Tomari', 'Yayoi', 'Laboratory of RNA Function']

有人能给予我一些关于将stdin读入CSV并稍后操作数据(只输出所需的列值、交换列等)的建议吗?

67up9zun

67up9zun1#

#!usr/bin/env python3
 import sys
 import csv

 data = sys.stdin.readlines()  # to read the file
 column_to_be_matched = int(data.pop(0)) # to get the column number to match
 word_to_be_matched = data.pop(0) # to get the word to be matched in said column
 col_headers = data.pop(0) # to get the column names
 print(", ".join(col_headers)) # to print the column names
 for line in csv.reader(data):
     if line[column_to_be_matched-1] == word_to_be_matched: #while it matched
        print(", ".join(line)) #print it
wfveoks0

wfveoks02#

使用Pandas读取和管理DataFrame中的数据

import pandas as pd
# File location
infile = r'path/file'
# Load file and skip first two rows
df = pd.read_csv(infile, skiprows=2)
# Refresh your Dataframe en throw out the rows that contain Kashiwa in the campus column
df = df[df['campus'] != 'Kashiwa']

您可以执行所有类型的编辑,例如,只需按以下方式对DataFrame进行排序:

df.sort(columns='your column')

检查Pandas documentation的所有可能性。

gr8qqesn

gr8qqesn3#

这是一种方法。

    • 例如:**
import csv

with open(filename) as csv_file:
    reader = csv.reader(csv_file)
    next(reader) #Skip First Line
    next(reader) #Skip Second Line
    print(next(reader)) #print Header
    for row in reader:
        if row[1] == 'Kashiwa':   #Filter By 'Kashiwa'
            print(row)
    • 输出:**
['Name', 'Campus', 'LabName']
['Shinichi MORISHITA', 'Kashiwa', 'Laboratory of Omics']
['Kiyoshi ASAI', 'Kashiwa', 'Laboratory of Genome Informatics']
c8ib6hqw

c8ib6hqw4#

import csv, sys

    f= sys.stdin.readline()
    data = csv.reader(f)

    out = []
    data_lines = list(data)
    for line in data_lines[2:5]:#u can increase index to match urs
        if line[1] == 'kashiwa':
            new = [line[0], line[1], line[2]]#u can use string instead if list
            string = f"{line[0]},{line[1]},{line[2]}"
            #print(string)#print does same as stdout u can use dis
            sys.stdout.write(string+'\n')
            out.append(new)

    sys.stdout.write(str(out))#same thing dat happens in print in the background#it out puts it as a list after the string repr

    #print(out)#u can use dis too instead of stdout

    f.close()

相关问题