0

この形式のテキスト ファイルにリストがあります。リストにはこのようなエントリが 1000 件あり、これは小さなサンプルです。

myitems =[
      ['some text, A', '12345'],
      ['place name 1', 'AAA'],
      ['some text, A', '12345'],
      ['some text', '12345'],
      ['some text CC', 'wwww'],
      ['new text', '12345'],
      ['other text, A', '12345'],
    ]

テキスト ファイルからリストを読み取って、このような出力を取得するにはどうすればよいですか。

newItems = [
  ['12345', 'some text, A'],
  ['AAA', 'place name 1'],
  ['12345', 'some text, A'],
  ['12345', 'some text'],
  ['wwww', 'some text CC'],
  ['12345', 'new text'],
  ['12345', 'other text, A'],
]

ファイルから読み取って文字列として操作することはできますが、リストとして取得するにはどうすればよいですか。個々のリスト アイテムにはコンマがある可能性があるため、コンマで区切ることはできません。

4

6 に答える 6

5

最も簡単なのはリスト内包表記です。

new_items = [i[::-1] for i in myitems]
于 2012-08-30T23:19:44.410 に答える
3
 newItems = [[b, a] for a, b in myitems]
于 2012-08-30T23:29:31.750 に答える
1
import sys

# Reading your file (the path has been passed in argument) and storing each lines in the array called "input"
input = []
with open(sys.argv[1], 'r') as all_lines:
    for line in all_lines:
        if line:
            input.append(line)

# Creating your array "newItems"
for j in input: 
    print j

newItems = []
if len(input)>2:
    for i in range(1, len(input)-1):
        # Getting content inside brackets. Ex: {{ 'some text, A', '12345' }}
        my_str = input[i].split("[")[1].split("]")[0]
        my_elements = my_str.split("'")

        # Appending elements
        newItems.append([my_elements[1], my_elements[3]])

print  newItems
于 2012-08-30T23:31:33.040 に答える
0

本当にそう見えるなら

 with open(my_file) as f:
   exec(f.read()) #read in the text and evaluate it in python creating myitems variable
   my_list = [reversed(l) for l in myitems]

exec は非常に危険であり、おそらく使用すべきではありません...しかし、これは機能するはずです

より良い解決策

 #name yourfile with the myitems = [...] to items.py
 import items
 new_items = [reversed(l) for l in items.myitems]
于 2012-08-30T23:16:50.547 に答える
0

ファイルを読み取るには、「個々の項目にコンマが含まれている可能性がある」と述べているため、CSV を使用します。

例:

ファイルが実際に次のようになっている場合:

'some text, A','12345'
'place name 1','AAA'
'some text, A','12345' 
'some text','12345'
'some text CC','wwww' 
'new text','12345'
'other text, A','12345'

次に、このコードは csv ファイルを読み取り、フィールドを逆にします。

import csv

with open('test.csv','rb') as csvIN:
    for row in csv.reader(csvIN, delimiter=',',quotechar="'"):
        print row
        row=row[::-1]
        print '==>',row,'\n'

版画:

['some text, A', '12345']
==> ['12345', 'some text, A'] 

['place name 1', 'AAA']
==> ['AAA', 'place name 1'] 

['some text, A', '12345 ']
==> ['12345 ', 'some text, A'] 

['some text', '12345']
==> ['12345', 'some text'] 

['some text CC', 'wwww ']
==> ['wwww ', 'some text CC'] 

['new text', '12345']
==> ['12345', 'new text'] 

['other text, A', '12345']
==> ['12345', 'other text, A'] 
于 2012-08-31T01:13:06.233 に答える
0
import re

f = open('myfile.txt', 'r')
newItems = []
for line in f:
    foo = re.search("\'(.*)\'.*\'(.*)\'", line) #we assume the values are 
                                                #in single quotes
    if foo is not None:
        a, b = foo.groups()
        newItems.append([b, a])
于 2012-08-30T23:39:53.187 に答える