如何在python

时间:2019-01-20 17:00:02

标签: python csv

我有此代码:

file=open("database.txt","r")
list_0=file.readlines()

item=input("enter station name: ")
for element in list_0:
    a=element.index(' ')
    if element.find(item.lower())!=-1:
        print(element[:a]+" -----> "+element[a:])

我想要从上面得到的结果

enter station name: ambala

14096 himalyan-queen

12498 shan-e-punjab

22461 shri-shakti

但是它显示了这个结果:

enter station name: ambala

14096 himalyan-queen new-delhi,ambala,chandigarh,kalka

12498 shan-e-punjab new-delhi,ambala,ludhiana,amritsar

22461 shri-shakti new-delhi,ambala,ludhiana,katra

有人知道该怎么做吗?

这是CSV文件中的数据(您可以看到分隔符为“-空格”

14096 himalyan-queen new-delhi,ambala,chandigarh,kalka
12498 shan-e-punjab new-delhi,ambala,ludhiana,amritsar
22461 shri-shakti new-delhi,ambala,ludhiana,katra
12412 intercity amritsar,ludhiana,chandigarh

7 个答案:

答案 0 :(得分:0)

尝试一下:

item=input("enter station name: ")
for element in list_0:
    a=element.split(' ')
    if element.find(item.lower())!=-1:
        print(a[0], a[1])

此修改将把您的输入与整个字符串进行比较,但只显示数字和字符串部分的第一部分,该部分由空格分隔。

答案 1 :(得分:0)

我建议使用csv library

ADD FOREIGN KEY

答案 2 :(得分:0)

问题是打印时您使用:

element[:a] // will print all the elements that are from the begining of the list until 'a' index

element[a:] // will print all the elements that are from 'a' index of the list until the end this is why you print all the elements 'himalyan-queen new-delhi,ambala,chandigarh,kalka'

要解决此问题,请尝试

file=open("database.txt","r")
list_0=file.readlines()

item=input("enter station name: ")
for element in list_0:
    splitted_elem=element.split(' ')
    if item.lower() in splitted_elem[2]:
        print(splitted_elem[0]+" -----> "+splitted_elem[1])

答案 3 :(得分:0)

with open('database.txt', 'r') as f:
    lines = f.readlines()

station_name = 'ambala'
for line in lines:
    spaced = line.split()
    commad = spaced[2].split(',')
    if station_name in commad[1]:
        print(spaced[0], '---->', spaced[1])

以上结果应为:

λ python sample.py
14096 ----> himalyan-queen
12498 ----> shan-e-punjab
22461 ----> shri-shakti

答案 4 :(得分:0)

以下是使用csv模块并使用“ with”语句打开和关闭文件的答案

import csv

with open('names.csv', newline='') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ')
    item=input("enter station name: ")
    for row in reader:
        if item in row[2].split(','):
            print(row[0], row[1])

答案 5 :(得分:0)

您应该找到第二个空格。

尝试以下代码:

file=open("C:\Users\Anything\Desktop\stackoverflow\db.txt","r")
list_0=file.readlines()

item=raw_input("enter station name: ")
for element in list_0:
    a=element.index(' ')
    if element.find(item.lower())!=-1:
        element1 = element[a:].strip()
        b = element1.index(' ')
        print (element[:a] + ' '+ element1[:b])

答案 6 :(得分:0)

正如其他人提到的那样,使用csv库,因此您可以读取列表中的行:

['14096 himalyan-queen new-delhi', 'ambala', 'chandigarh', 'kalka']

然后,您可以轻松地按索引访问所需的列。

这是另一种可能的实现方式:

import csv

item = 'ambala' # input etc.
with open('database.txt', newline='') as csvfile:
  reader = csv.reader(csvfile)
  for row in reader:
    if row[1] == item: # look just on the second column, but you can customise: if item in row[1:]:
      a = row[0].index(' ')
      print((row[0][:a] + " -----> " + row[0][a:]))

# 14096 ----->  himalyan-queen new-delhi
# 12498 ----->  shan-e-punjab new-delhi
# 22461 ----->  shri-shakti new-delhi