[英]how to print a specific line of a csv file in python based on user input?
Currently when the user inputs a number, rather than printing the text in that line it prints all numeric values.当前,当用户输入一个数字时,它不会打印该行中的文本,而是打印所有数值。
file = open ("movies-wk5.csv")
print("type a number between 1 and 158")
movie_ID = input()
for response in file:
datalist = response.split(",")
movie_ID = datalist[0]
readline(movie_ID)
You can use a range
with your user input to skip to the line desired:您可以在用户输入中使用range
以跳到所需的行:
# Convert this to an int
movie_id = int(input("Type a number between 1 and 158: "))
with open("movies-wk5.csv") as fh:
# need a -1 here because items are 0 indexed
for i in range(movie_id - 1):
# next() will consume the next line in the file
_ = next(fh)
# grab the line you wanted
line = next(fh)
print(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.