英文:
How do I read from a text file a number of sequential strings and assign each of them to a variable that is an array in Python 3.8?
问题
Define a variable as an array:
在其他语言中,我会编写类似以下的代码来实现这个:
将变量定义为数组:
对于这个简单的问题,任何帮助都将不胜感激。我知道可以使用"import array",但我需要一些示例代码来帮助我开始。
提前感谢大家。
myfile.txt:(在生产环境中可能会有超过1,000,000行。这些是用于测试的垃圾行。)
========================================================================
"0x28ce2ae","这是第四次测试","文件系统","06/03/2023"
"0x232d37d2","fskjg k jmlsj m lm","选择","06/10/2023"
"0x2f79d6f8","oskjfjommmskol","完整","06/02/2022"
"0x2f775822","这是一个测试测试测试","选择","01/04/20023"
"0xca21103","nosomgfmosfmjki","文件系统","05/05/20023"
"0x13ec8751","fhgghhhfgfhghggfhfhfhhhhhffhh","恢复","06/07/2023"
"0X1ADB4922","测试","完整","06/24/2023"
"0X262B2A15","只是东西","文件系统","06/27/2023"
"0X49CB95E4EDBE","测试大测试","恢复","06/27/2023"
"0X39CDECD2CFD5","这是一个愚蠢测试","选择","06/28/2023"
========================================================================
usbNum=[]
shortDescription = []
backupType = []
dateTaken = []
file = open("myfile.txt","r")
print()
#对文本文件中的每个USB备份重复执行。
for line in file:
#让我们使用"\",""作为分隔符将行拆分为名为"fields"的数组:
fields = line.split("\",\"")
#并且让我们提取数据:
usbNum = fields[0].replace('\"','').upper()
shortDescription = fields[1]
backupType = fields[2]
dateTaken = fields[3].replace('\"','')
# 在终端上显示数据。
print( usbNum + " 拍摄于 " + dateTaken + " 用于 " + backupType + ".")
file.close()
====================================================================================
英文:
In other languages, I would write code similar to the following to achieve this:
Define a variable as an array :
Any help with this simple matter would be appreciated. I know that there is
"import array" I could use, but I need help with some example code to help me get jump started.
Thank you all a head of time.
myfile.txt: (there will likely be over 1,000,000 rows in a production environment. These are junk rows for testing.)
========================================================================
"0x28ce2ae","This is a forth test","filesys","06/03/2023"
"0x232d37d2","fskjg k jmlsj m lm","select","06/10/2023"
"0x2f79d6f8","oskjfjommmskol","full","06/02/2022"
"0x2f775822","this is a testtesttest","select","01/04/20023"
"0xca21103","nosomgfmosfmjki","filesys","05/05/20023"
"0x13ec8751","fhgghhhfgfhghggfhfhfhhhhhffhh","recovery","06/07/2023"
"0X1ADB4922","test","full","06/24/2023"
"0X262B2A15","Just stuff","filesys","06/27/2023"
"0X49CB95E4EDBE","test large test","recovery","06/27/2023"
"0X39CDECD2CFD5","this is a test of stupidity","select","06/28/2023"
========================================================================
usbNum=[]
shortDescription = []
backupType = []
dateTaken = []
file = open("myfile.txt","r")
print()
#Repeat for each USB backup in the text file.
for line in file:
#Let's split the line into an array called "fields" using the "\",\"" as a separator:
fields = line.split("\",\"")
#and let's extract the data:
usbNum = fields[0].replace('"','').upper()
shortDescription = fields[1]
backupType = fields[2]
dateTaken = fields[3].replace('"','')
# Display the data to the terminal.
print( usbNum + " taken " + dateTaken + " for " + backupType + ".")
file.close()
====================================================================================
答案1
得分: 1
我认为这个代码可以工作:
usbNum = []
shortDescription = []
backupType = []
dateTaken = []
with open(r"C:\Users\Admin\Downloads.txt", "r") as f:
f.seek(0)
for line in f:
fields: list[str] = line.split(",")
usbNum.append(fields[0].strip('"').upper())
shortDescription.append(fields[1].strip('"'))
backupType.append(fields[2].strip('"'))
dateTaken.append(fields[3].strip('"').strip("\n"))
print(usbNum, shortDescription, backupType, dateTaken)
感谢Matthias的评论,我进行了一些优化:
import csv
with open(r"C:\Users\Admin\Downloads.txt", "r") as f:
rows = csv.reader(f)
# 顺便说一下,如果usbNum是唯一的,我同意这里使用字典可能更好...
backups = {row[0].upper(): {"shortDescription": row[1], "backupType": row[2], "dateTaken": row[3]} for row in rows} # usbNum: {shortDescription: xxx, backupType: xxx, dateTaken: xxx}
print(backups)
英文:
I suppose this could work:
usbNum = []
shortDescription = []
backupType = []
dateTaken = []
with open(r"C:\Users\Admin\Downloads.txt", "r") as f:
f.seek(0)
for line in f:
fields: list[str] = line.split(",")
usbNum.append(fields[0].strip("\"").upper())
shortDescription.append(fields[1].strip("\""))
backupType.append(fields[2].strip("\""))
dateTaken.append(fields[3].strip("\"").strip("\n"))
print(usbNum, shortDescription, backupType, dateTaken)
Thanks for Matthias's comment, I did some optimization:
import csv
with open(r"C:\Users\Admin\Downloads.txt", "r") as f:
rows = csv.reader(f)
# by the way, I agree here a dict might be better if the usbNum is unique...
backups = {row[0].upper(): {"shortDescription": row[1], "backupType": row[2], "dateTaken": row[3]} for row in rows} # usbNum: {shortDescription: xxx, backupType: xxx, dateTaken: xxx}
print(backups)
答案2
得分: 0
非常感谢大家!我现在有一个解决方案,并认为这个问题已经解决。我已经实现了Matthias提供的解决方案。我学到了很多。作为一个纯粹的C环境出身,Python有时候让人摸不着头脑。哈哈!
这是解决方案:
usbNum = []
shortDescription = []
backupType = []
dateTaken = []
with open(r"C:\Users\Admin\Downloads.txt", "r") as f:
f.seek(0)
for line in f:
fields: list[str] = line.split(",")
usbNum.append(fields[0].strip("\"").upper())
shortDescription.append(fields[1].strip("\""))
backupType.append(fields[2].strip("\""))
dateTaken.append(fields[3].strip("\"").strip("\n"))
英文:
Thank you all so much! I have a solution now and considered this closed now. I have implemented the solution provided by Matthias. I learned so much. Coming from a purely C environment, Python doesn't always make sense. LOL!
This is the solution:
usbNum = []
shortDescription = []
backupType = []
dateTaken = []
with open(r"C:\Users\Admin\Downloads.txt", "r") as f:
f.seek(0)
for line in f:
fields: list[str] = line.split(",")
usbNum.append(fields[0].strip("\"").upper())
shortDescription.append(fields[1].strip("\""))
backupType.append(fields[2].strip("\""))
dateTaken.append(fields[3].strip("\"").strip("\n"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论