실제로 동일한 목록에 3 개의 다른 데이터 문자열을 저장하는 코드가 있습니다. 각 목록이 분리되고 난 그래서 난 그냥 코드를 붙여거야 ... 그 감각을 만든 경우도 확실하지 않다 :Python의 JSON을 사용하여 파일에서 특정 목록의 특정 문자열을 검색하는 방법
filename = "datosdeusuario.txt"
leyendo = open(filename, 'r+')
buffer = leyendo.read()
leyendo.close()
if not user.name in buffer:
escribiendo = open(filename, 'a')
escribiendo.write(json.dumps([user.name, string2, string3])+"\n")
escribiendo.close()
print(str(horaactual)[:19] + ": Data from " + user.name + " added")
그 코드는 다음과 같은 정보를 저장 (거의 완벽한 작업)한다 :
["saelyth", "thisisanexampleforstring2", "andthisisanexampleforstring3"]
["anothername", "thisisanothernexampleforstring2", "andthisisanotherexampleforstring3"]
["John Connor", "exampleforstring2", "exampleforstring3"]
그래서 내 실제 질문 및 문제는 ... 그 파일에서 특정 문자열을 가져 오는 가장 좋은 방법은 무엇입니까? 예를 들어
,의 내가 첫 번째 문자열, 두 번째 문자열 만 경우는 user.name은 (내 말은, 이름이 목록에서 모든 세 가지 값 John Connor
입니다 세 번째 문자열을 검색 할 가정 해 봅시다 녹이다). 인터넷 검색을 수행하는 적절한 방법을 찾지 못하는 것 같습니다. 예상되는 코드는 다음과 같습니다.
if user.name is in found in any list(position1) of the file datosdeusuario.txt
then
retrieveddata1 = List where value1 is user.name(get value1)
retrieveddata2 = List where value1 is user.name(get value2)
retrieveddata3 = List where value1 is user.name(get value3)
나는 그 방법을 모릅니다. 그래서 그 상황을 설명하기 위해이 코드를 작성했습니다.
오, 그래 : D 정확하게 작동합니다. 빠른 답변을 주셔서 대단히 감사합니다, 나는 잠시 붙어있었습니다. – Saelyth
@Saelyth 님이 문제를 해결하면 답변을 수락하는 것이 좋습니다. –