所以我遇到了困难。我正在尝试使用python建立本地数据库,但要尽量减少用户输入。我有一个脚本,可以使带有特定标头{id,name,details}的csv文件,以及一个可以根据需要检索特定行的脚本。
import csv
ID = input("Input ID Number :")
File_Name = str(ID)+str(".csv")
with open(File_Name, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
rows = list(reader)
print(rows[1])
假设File_Name存在,并且目标是获取第2行第2列中的数据。此数据将用于以后进一步追加文件:)
问题:我将如何从这里获得第二列中的数据。
答案 0 :(得分:1)
>>>l = [(1),(2)]
>>>schema=T.StructType([T.StructField('task_id',T.IntegerType(),True)])
>>>spark.createDataFrame(l,schema=schema).show()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tianlh/spark/python/pyspark/sql/session.py", line 526, in createDataFrame
rdd, schema = self._createFromLocal(map(prepare, data), schema)
File "/home/tianlh/spark/python/pyspark/sql/session.py", line 387, in _createFromLocal
data = list(data)
File "/home/tianlh/spark/python/pyspark/sql/session.py", line 509, in prepare
verify_func(obj, schema)
File "/home/tianlh/spark/python/pyspark/sql/types.py", line 1366, in _verify_type
raise TypeError("StructType can not accept object %r in type %s" % (obj, type(obj)))
TypeError: StructType can not accept object 1 in type <type 'int'>
import csv
ID = input("Input ID Number :")
File_Name = str(ID)+ ".csv"
with open(File_Name, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=' ', quotechar='|')
rows = list(reader)
print(rows[1][1])
12 09 022
14 15 212
答案 1 :(得分:0)
您可以使用pandas
阅读特定的单元格:
import pandas as pd
id_ = input('Input ID Number: ')
df = pd.read_csv('{}.csv'.format(id_), header=None, delimiter=' ')
print(df.iat[1, 1])
假设我们关注file.csv
:
1 2 3
4 5 6
7 8 9
结果将是:
Input ID Number: file
5
答案 2 :(得分:0)
我也最终服用
column = rows [1]从数组中获取第二列值。谢谢大家