我想每次将变量写入文件并换行。我见过的所有修复程序均无效。我知道使用'\ n'并将其添加到变量中,但实际上并没有做任何事情。
file.write(searchString + '\n')
file.write(searchString)
file.write('\n')
file.write('searchString \n')
以上是我尝试添加新行的内容 下面是我的actaul代码,我需要将新的行修复程序实现为
import csv
searchString = input("Please enter a string to be searched through: ")
fileList = ["prices.csv", "pricessplit.csv", "securities.csv", "fundamentals.csv"]
file = open(searchString+ ".txt", "a", newline="")
file.write(searchString + '\n')
index = 0
fileType = []
for i in fileList:
with open(fileList[index], "r") as b:
file.write(fileList[index] + '\n')
reader = csv.reader(b, delimiter=",",)
for row in reader:
for field in row:
if field == searchString:
print(row)
file.write(str(row))
file.write("\n")
index = index + 1
我希望它在每次输入数据后都在带有新行的文本文件中
我正在使用stovks,所以我希望它显示searchString-新行
然后是prices.csv
然后在写完每行的所有数据后显示所有库存,但是要换行
预期输出应为:
OKE
prices.csv
['2010-01-04', 'OKE', '44.880002', '45.579999', '44.880002', '45.709999', '1759600.0']
['2010-01-05', 'OKE', '45.500002', '45.18', '44.589999', '45.749998', '3324900.0']
['2010-01-06', 'OKE', '45.270001', '45.44', '45.000002', '45.530001', '1769400.0']
['2010-01-07', 'OKE', '45.510002', '45.140001', '44.839999', '45.510002', '1609800.0']
['2010-01-08', 'OKE', '45.109998', '45.400001', '44.770001', '45.449999', '1061300.0']
['2010-01-11', 'OKE', '45.68', '46.410001', '45.44', '46.510002', '1793000.0']
['2010-01-12', 'OKE', '46.209999', '45.729998', '45.56', '46.369998', '1631200.0']
['2010-01-13', 'OKE', '45.989998', '45.92', '45.55', '45.989998', '876900.0']
但是我得到的是:
OKEprices.csv['2010-01-04', 'OKE', '44.880002', '45.579999', '44.880002', '45.709999', '1759600.0']
['2010-01-05', 'OKE', '45.500002', '45.18', '44.589999', '45.749998', '3324900.0']
['2010-01-06', 'OKE', '45.270001', '45.44', '45.000002', '45.530001', '1769400.0']
['2010-01-07', 'OKE', '45.510002', '45.140001', '44.839999', '45.510002', '1609800.0']
['2010-01-08', 'OKE', '45.109998', '45.400001', '44.770001', '45.449999', '1061300.0']