我有一个包含不同单词行的文本文件(.txt) 例如:
SELECT SFLIGHT-CARRID SFLIGHT-CONNID SFLIGHT-FLDATE
AIRPFROM AIRPTO
DEPTIME
SEATSMAX SEATSOCC
FROM SPFLI " connections
INNER JOIN SFLIGHT " flights
ON SPFLI~CARRID = SFLIGHT~CARRID
AND SPFLI~CONNID = SFLIGHT~CONNID
INNER JOIN SCITAIRP AS F " Mapping cities to
ON SPFLI~AIRPFROM = F~AIRPORT " airports
INNER JOIN SCITAIRP AS T
ON SPFLI~AIRPTO = T~AIRPORT
WHERE F~COUNTRY = FROMCOUNTRYKEY " restrictions
AND F~CITY = FROMCITY
AND T~COUNTRY = TOCOUNTRYKEY
AND T~CITY = TOCITY
**AND FLDATE >= SY-DATUM**
AND DEPTIME > AFTERNOON_HLP
AND SPFLI~CARRID IN CARRID
ORDER BY FLDATE AIRPFROM AIRPTO.
如何使用 Multimap<String, String> map = ArrayListMultimap.create();
map.put("MiddleName", null);
map.putAll("EmailAddress", ImmutableList.of("toni@gmail.com", "mary@gmail.com", "paul@gmail.com"));
// that's the key with the "biggest" collection within the map
int biggest = map.asMap().entrySet().stream().collect(Collectors.reducing(0, entry -> entry.getValue().size(), Integer::max));
Multimap<String, String> newMap = ArrayListMultimap.create();
// "padding" the collection when required
map.keySet().stream().forEach(key -> {
int currentSize = map.get(key).size();
newMap.putAll(key, map.get(key));
if (currentSize < biggest) {
newMap.putAll(key, Collections.nCopies(biggest - currentSize, (String) null));
}
});
System.out.println(newMap); // {MiddleName=[null, null, null], EmailAddress=[toni@gmail.com, mary@gmail.com, paul@gmail.com]}
}
命令获取第2列...所以2和12例如是文本?
答案 0 :(得分:4)
假设,
周围没有空格:
with open('file.txt') as f:
for line in f:
print(line.split(',')[1])
如果可能有空格,请使用re.split()
:
with open('file.txt') as f:
for line in f:
print(re.split(r'\s?,\s?', line)[1])
答案 1 :(得分:2)
首先,您必须将文件拆分为线条(只需通过迭代即可轻松完成),而不必从每一行中提取第二列。
简单示例:
with open("myfile.txt") as myfile:
for line in myfile:
print(line.split(",")[1]) # indexing starts at 0
累积值列表:
with open("myfile.txt") as myfile:
mylist = [line.split(",")[1] for line in myfile]